Merge pull request #1117 from trheyi/main

Refactor document ID handling in API responses
This commit is contained in:
Max 2025-08-21 11:11:36 +08:00 committed by GitHub
commit d4d19cfe60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 105 additions and 63 deletions

View file

@ -74,7 +74,7 @@ func GetSegmentGraph(c *gin.Context) {
result := gin.H{
"entities": []interface{}{},
"relationships": []interface{}{},
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"options": options,
}
@ -130,7 +130,7 @@ func ExtractSegmentGraph(c *gin.Context) {
// Return mock response for now
result := gin.H{
"message": "Entities and relationships extracted successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"entities_count": 0,
"relationships_count": 0,
@ -198,7 +198,7 @@ func ExtractSegmentGraphAsync(c *gin.Context) {
result := gin.H{
"job_id": jobID,
"message": "Graph extraction started",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
}

View file

@ -37,7 +37,7 @@ func ScrollHits(c *gin.Context) {
// Parse query parameters for scroll options
options := map[string]interface{}{
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"limit": 100, // Default limit
}
@ -146,7 +146,7 @@ func GetHits(c *gin.Context) {
// Return mock response for now
result := gin.H{
"hits": []interface{}{},
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"total": 0,
}
@ -200,7 +200,7 @@ func GetHit(c *gin.Context) {
// TODO: Implement get hit detail logic
c.JSON(http.StatusOK, gin.H{
"hit": nil,
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"hit_id": hitID,
})
@ -234,7 +234,7 @@ func AddHits(c *gin.Context) {
// TODO: Implement add hit logic
c.JSON(http.StatusOK, gin.H{
"message": "Hit added successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"hit_id": "placeholder-hit-id",
})
@ -281,7 +281,7 @@ func UpdateHits(c *gin.Context) {
result := gin.H{
"message": "Hits updated successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"updated_count": len(hitIDs),
}
@ -352,7 +352,7 @@ func RemoveHits(c *gin.Context) {
result := gin.H{
"message": "Hits removed successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"hit_ids": validHitIDs,
"removed_count": len(validHitIDs),

View file

@ -70,7 +70,7 @@ func UpdateScores(c *gin.Context) {
result := gin.H{
"message": "Scores updated successfully",
"document_id": docID,
"doc_id": docID,
"scores": req.Scores,
"updated_count": len(req.Scores),
}

View file

@ -331,7 +331,7 @@ func RemoveSegments(c *gin.Context) {
}
// Perform remove segments operation
removedCount, err := kb.Instance.RemoveSegments(c.Request.Context(), validSegmentIDs)
removedCount, err := kb.Instance.RemoveSegments(c.Request.Context(), docID, validSegmentIDs)
if err != nil {
errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code,
@ -412,7 +412,7 @@ func GetSegments(c *gin.Context) {
// TODO: Implement get segments logic
c.JSON(http.StatusOK, gin.H{
"segments": []interface{}{},
"document_id": docID,
"doc_id": docID,
})
}
@ -440,13 +440,55 @@ func GetSegment(c *gin.Context) {
return
}
// Check if KB instance exists
if kb.Instance == nil {
errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code,
ErrorDescription: "Knowledge base instance is not initialized",
}
response.RespondWithError(c, response.StatusInternalServerError, errorResp)
return
}
// TODO: Implement document permission validation for docID
// TODO: Implement get single segment logic
c.JSON(http.StatusOK, gin.H{
"segment": nil,
"document_id": docID,
// Get the segment using KB interface
segment, err := kb.Instance.GetSegment(c.Request.Context(), docID, segmentID)
if err != nil {
errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code,
ErrorDescription: fmt.Sprintf("Failed to get segment: %v", err),
}
response.RespondWithError(c, response.StatusInternalServerError, errorResp)
return
}
if segment == nil {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: "Segment not found",
}
response.RespondWithError(c, response.StatusNotFound, errorResp)
return
}
// Verify that the segment belongs to the specified document
if segment.DocumentID != docID {
errorResp := &response.ErrorResponse{
Code: response.ErrAccessDenied.Code,
ErrorDescription: "Segment does not belong to the specified document",
}
response.RespondWithError(c, response.StatusForbidden, errorResp)
return
}
result := gin.H{
"segment": segment,
"doc_id": docID,
"segment_id": segmentID,
})
}
response.RespondWithSuccess(c, response.StatusOK, result)
}
// ScrollSegments scrolls segments with iterator-style pagination
@ -623,7 +665,7 @@ func AddSegmentsAsync(c *gin.Context) {
result := gin.H{
"job_id": jobID,
"message": "Segments addition started",
"document_id": docID,
"doc_id": docID,
}
response.RespondWithSuccess(c, response.StatusCreated, result)
@ -682,7 +724,7 @@ func UpdateSegmentsAsync(c *gin.Context) {
result := gin.H{
"job_id": jobID,
"message": "Segments update started",
"document_id": docID,
"doc_id": docID,
}
response.RespondWithSuccess(c, response.StatusCreated, result)
@ -748,7 +790,7 @@ func GetSegmentParents(c *gin.Context) {
// Return mock response for now
result := gin.H{
"parents": []interface{}{},
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"depth": depth,
"total": 0,

View file

@ -37,7 +37,7 @@ func ScrollVotes(c *gin.Context) {
// Parse query parameters for scroll options
options := map[string]interface{}{
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"limit": 100, // Default limit
}
@ -140,7 +140,7 @@ func GetVotes(c *gin.Context) {
// Return mock response for now
result := gin.H{
"votes": []interface{}{},
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"total": 0,
}
@ -194,7 +194,7 @@ func GetVote(c *gin.Context) {
// TODO: Implement get vote detail logic
c.JSON(http.StatusOK, gin.H{
"vote": nil,
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"vote_id": voteID,
})
@ -228,7 +228,7 @@ func AddVotes(c *gin.Context) {
// TODO: Implement add vote logic
c.JSON(http.StatusOK, gin.H{
"message": "Vote added successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"vote_id": "placeholder-vote-id",
})
@ -275,7 +275,7 @@ func UpdateVotes(c *gin.Context) {
result := gin.H{
"message": "Votes updated successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"updated_count": len(voteIDs),
}
@ -346,7 +346,7 @@ func RemoveVotes(c *gin.Context) {
result := gin.H{
"message": "Votes removed successfully",
"document_id": docID,
"doc_id": docID,
"segment_id": segmentID,
"vote_ids": validVoteIDs,
"removed_count": len(validVoteIDs),

View file

@ -56,7 +56,7 @@ func UpdateWeights(c *gin.Context) {
// TODO: Implement document permission validation for docID
// Perform batch update weight operation
updatedCount, err := kb.Instance.UpdateWeight(c.Request.Context(), req.Weights)
updatedCount, err := kb.Instance.UpdateWeight(c.Request.Context(), docID, req.Weights)
if err != nil {
errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code,
@ -69,7 +69,7 @@ func UpdateWeights(c *gin.Context) {
// Return success response
result := gin.H{
"message": "Segment weights updated successfully",
"document_id": docID,
"doc_id": docID,
"weights": req.Weights,
"updated_count": updatedCount,
}