Refactor segment handling to improve pagination and endpoint clarity

- Replaced ListSegments function with ScrollSegments to implement iterator-style pagination.
- Updated related comments and error messages to reflect the new scrolling functionality.
- Streamlined API routes by removing the redundant ListSegments endpoint, enhancing overall clarity.
This commit is contained in:
Max 2025-08-15 16:43:22 +08:00
parent 451569e8cb
commit c4c598db35
2 changed files with 11 additions and 20 deletions

View file

@ -44,8 +44,7 @@ func Attach(group *gin.RouterGroup, oauth types.OAuth) {
group.DELETE("/documents/:docID/segments", RemoveSegmentsByDocID) group.DELETE("/documents/:docID/segments", RemoveSegmentsByDocID)
group.GET("/segments", GetSegments) group.GET("/segments", GetSegments)
group.GET("/segments/:segmentID", GetSegment) group.GET("/segments/:segmentID", GetSegment)
group.GET("/documents/:docID/segments", ListSegments) group.GET("/documents/:docID/segments", ScrollSegments)
group.GET("/documents/:docID/segments/scroll", ScrollSegments)
// Segment Voting, Scoring, Weighting // Segment Voting, Scoring, Weighting
group.PUT("/segments/vote", UpdateVote) group.PUT("/segments/vote", UpdateVote)

View file

@ -172,8 +172,8 @@ func GetSegment(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"segment": nil}) c.JSON(http.StatusOK, gin.H{"segment": nil})
} }
// ListSegments lists segments with pagination // ScrollSegments scrolls segments with iterator-style pagination
func ListSegments(c *gin.Context) { func ScrollSegments(c *gin.Context) {
// Parse docID from URL path parameter // Parse docID from URL path parameter
docID := c.Param("docID") docID := c.Param("docID")
if docID == "" { if docID == "" {
@ -195,8 +195,8 @@ func ListSegments(c *gin.Context) {
return return
} }
// Parse query parameters for pagination and filtering // Parse query parameters for scroll options
options := &types.ListSegmentsOptions{ options := &types.ScrollSegmentsOptions{
IncludeMetadata: true, // Default to include metadata IncludeMetadata: true, // Default to include metadata
} }
@ -210,11 +210,9 @@ func ListSegments(c *gin.Context) {
options.Limit = 100 // Default limit options.Limit = 100 // Default limit
} }
// Parse offset (default: 0) // Parse scroll_id parameter for continuing pagination
if offsetStr := c.Query("offset"); offsetStr != "" { if scrollID := strings.TrimSpace(c.Query("scroll_id")); scrollID != "" {
if offset, err := strconv.Atoi(offsetStr); err == nil && offset >= 0 { options.ScrollID = scrollID
options.Offset = offset
}
} }
// Parse order_by parameter // Parse order_by parameter
@ -267,12 +265,12 @@ func ListSegments(c *gin.Context) {
options.Filter = filter options.Filter = filter
} }
// Call GraphRag ListSegments method // Call GraphRag ScrollSegments method
result, err := kb.Instance.ListSegments(c.Request.Context(), docID, options) result, err := kb.Instance.ScrollSegments(c.Request.Context(), docID, options)
if err != nil { if err != nil {
errorResp := &response.ErrorResponse{ errorResp := &response.ErrorResponse{
Code: response.ErrServerError.Code, Code: response.ErrServerError.Code,
ErrorDescription: "Failed to list segments: " + err.Error(), ErrorDescription: "Failed to scroll segments: " + err.Error(),
} }
response.RespondWithError(c, response.StatusInternalServerError, errorResp) response.RespondWithError(c, response.StatusInternalServerError, errorResp)
return return
@ -281,9 +279,3 @@ func ListSegments(c *gin.Context) {
// Return success response // Return success response
response.RespondWithSuccess(c, response.StatusOK, result) response.RespondWithSuccess(c, response.StatusOK, result)
} }
// ScrollSegments scrolls segments with iterator-style pagination
func ScrollSegments(c *gin.Context) {
// TODO: Implement scroll segments logic
c.JSON(http.StatusOK, gin.H{"segments": []interface{}{}, "cursor": ""})
}