Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(feishu): stop Wiki pagination on repeated page tokens (#3645)

xxxxx5614c04d88f2
diff --git a/internal/datasource/connector/feishu/core/client.go b/internal/datasource/connector/feishu/core/client.go
index 52d12e3f7..d77e3941f 100644
--- a/internal/datasource/connector/feishu/core/client.go
+++ b/internal/datasource/connector/feishu/core/client.go
@@ -304,6 +304,7 @@ func truncate(s string, maxLen int) string {
func (c *Client) ListWikiSpaces(ctx context.Context) ([]WikiSpace, error) {
var allSpaces []WikiSpace
pageToken := ""
+ seenPageTokens := make(map[string]struct{})
for {
path := "/open-apis/wiki/v2/spaces?page_size=50"
@@ -330,6 +331,10 @@ func (c *Client) ListWikiSpaces(ctx context.Context) ([]WikiSpace, error) {
if !resp.Data.HasMore || resp.Data.PageToken == "" {
break
}
+ if _, exists := seenPageTokens[resp.Data.PageToken]; exists {
+ return nil, fmt.Errorf("feishu wiki space pagination repeated page token %q", resp.Data.PageToken)
+ }
+ seenPageTokens[resp.Data.PageToken] = struct{}{}
pageToken = resp.Data.PageToken
}
@@ -342,6 +347,7 @@ func (c *Client) ListWikiSpaces(ctx context.Context) ([]WikiSpace, error) {
func (c *Client) ListWikiNodes(ctx context.Context, spaceID string, parentNodeToken string) ([]WikiNode, error) {
var allNodes []WikiNode
pageToken := ""
+ seenPageTokens := make(map[string]struct{})
for {
path := fmt.Sprintf("/open-apis/wiki/v2/spaces/%s/nodes?page_size=50", spaceID)
@@ -373,6 +379,10 @@ func (c *Client) ListWikiNodes(ctx context.Context, spaceID string, parentNodeTo
if !resp.Data.HasMore || resp.Data.PageToken == "" {
break
}
+ if _, exists := seenPageTokens[resp.Data.PageToken]; exists {
+ return nil, fmt.Errorf("feishu wiki node pagination repeated page token %q", resp.Data.PageToken)
+ }
+ seenPageTokens[resp.Data.PageToken] = struct{}{}
pageToken = resp.Data.PageToken
}
diff --git a/internal/datasource/connector/feishu/wiki/connector_test.go b/internal/datasource/connector/feishu/wiki/connector_test.go
index 305624a9e..32999eb4f 100644
--- a/internal/datasource/connector/feishu/wiki/connector_test.go
+++ b/internal/datasource/connector/feishu/wiki/connector_test.go
@@ -1205,6 +1205,86 @@ func TestClientListWikiSpaces(t *testing.T) {
}
}
+func TestClientWikiPaginationRejectsRepeatedPageToken(t *testing.T) {
+ tests := []struct {
+ name string
+ path string
+ response func(int) interface{}
+ list func(*core.Client) error
+ }{
+ {
+ name: "spaces",
+ path: "/open-apis/wiki/v2/spaces",
+ response: func(requests int) interface{} {
+ return core.WikiSpaceListResponse{
+ ApiResponse: core.ApiResponse{Code: 0},
+ Data: core.WikiSpaceListData{
+ Items: []core.WikiSpace{{SpaceID: fmt.Sprintf("space-%d", requests)}},
+ HasMore: true,
+ PageToken: "repeated-token",
+ },
+ }
+ },
+ list: func(client *core.Client) error {
+ _, err := client.ListWikiSpaces(context.Background())
+ return err
+ },
+ },
+ {
+ name: "nodes",
+ path: "/open-apis/wiki/v2/spaces/space1/nodes",
+ response: func(requests int) interface{} {
+ return core.WikiNodeListResponse{
+ ApiResponse: core.ApiResponse{Code: 0},
+ Data: core.WikiNodeListData{
+ Items: []core.WikiNode{{NodeToken: fmt.Sprintf("node-%d", requests)}},
+ HasMore: true,
+ PageToken: "repeated-token",
+ },
+ }
+ },
+ list: func(client *core.Client) error {
+ _, err := client.ListWikiNodes(context.Background(), "space1", "")
+ return err
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ mux := http.NewServeMux()
+ authPath := "/open-apis/auth/v3/tenant_access_token/internal"
+ mux.HandleFunc(authPath, func(w http.ResponseWriter, _ *http.Request) {
+ writeJSON(w, core.TokenResponse{
+ ApiResponse: core.ApiResponse{Code: 0},
+ TenantAccessToken: "fake-token",
+ Expire: 7200,
+ })
+ })
+ requests := 0
+ mux.HandleFunc(tt.path, func(w http.ResponseWriter, _ *http.Request) {
+ requests++
+ if requests > 2 {
+ http.Error(w, "pagination should have stopped", http.StatusBadGateway)
+ return
+ }
+ writeJSON(w, tt.response(requests))
+ })
+ ts := httptest.NewServer(mux)
+ defer ts.Close()
+
+ client := core.NewClient(&core.Config{AppID: "a", AppSecret: "b", BaseURL: ts.URL})
+ err := tt.list(client)
+ if err == nil || !strings.Contains(err.Error(), "repeated page token") {
+ t.Fatalf("expected repeated-page-token error, got %v", err)
+ }
+ if requests != 2 {
+ t.Fatalf("list requests = %d, want 2", requests)
+ }
+ })
+ }
+}
+
// ──────────────────────────────────────────────────────────────────────
// Type mapping tests
// ──────────────────────────────────────────────────────────────────────