Merge pull request #1113 from trheyi/main

Refactor segment handling to improve pagination and endpoint clarity
This commit is contained in:
Max 2025-08-15 16:44:31 +08:00 committed by GitHub
commit 6fa46be875
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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.GET("/segments", GetSegments)
group.GET("/segments/:segmentID", GetSegment)
group.GET("/documents/:docID/segments", ListSegments)
group.GET("/documents/:docID/segments/scroll", ScrollSegments)
group.GET("/documents/:docID/segments", ScrollSegments)
// Segment Voting, Scoring, Weighting
group.PUT("/segments/vote", UpdateVote)

View file

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