Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(feishu): sync child documents of wiki shortcuts (#4565)
Lyang77f0e93abafa7f
diff --git a/internal/datasource/connector/feishu/core/types.go b/internal/datasource/connector/feishu/core/types.goindex be0c5e2b5..c41b875d4 100644--- a/internal/datasource/connector/feishu/core/types.go+++ b/internal/datasource/connector/feishu/core/types.go@@ -177,7 +177,7 @@ type WikiNode struct {ObjType string `json:"obj_type"` // "doc", "sheet", "mindnote", "bitable", "file", "docx", "slides"ParentNodeID string `json:"parent_node_token"`NodeType string `json:"node_type"` // "origin" or "shortcut"- OriginNodeID string `json:"origin_node_id"`+ OriginNodeID string `json:"origin_node_token"`OriginSpaceID string `json:"origin_space_id"`HasChild bool `json:"has_child"`Title string `json:"title"`diff --git a/internal/datasource/connector/feishu/core/wiki_shortcuts.go b/internal/datasource/connector/feishu/core/wiki_shortcuts.gonew file mode 100644index 000000000..9afa0e9e6--- /dev/null+++ b/internal/datasource/connector/feishu/core/wiki_shortcuts.go@@ -0,0 +1,145 @@+package core++import (+ "context"+ "errors"+ "fmt"++ "github.com/Tencent/WeKnora/internal/logger"+)++// ListWikiNodesWithShortcuts supplements the existing traversal with descendants+// of shortcuts. The original traversal and its partial failures are preserved.+func (c *Client) ListWikiNodesWithShortcuts(ctx context.Context, spaceID, nodeToken string) ([]WikiNode, error) {+ nodes, err := c.ListWikiNodesRecursiveFrom(ctx, spaceID, nodeToken)+ var failures []WikiNodeListFailure+ if err != nil {+ var partial *PartialWikiNodeListError+ if !errors.As(err, &partial) {+ return nodes, err+ }+ failures = append(failures, partial.Failures...)+ }+ return c.expandWikiShortcuts(ctx, spaceID, nodes, failures)+}++func (c *Client) expandWikiShortcuts(+ ctx context.Context, spaceID string, nodes []WikiNode, failures []WikiNodeListFailure,+) ([]WikiNode, error) {+ if err := ctx.Err(); err != nil {+ return nodes, err+ }+ // Existing nodes have already been emitted. The original traversal handles+ // ordinary nodes and shortcuts reporting children; only unexpanded shortcuts+ // need processing in this extension.+ seen := make(map[string]bool)+ processed := make(map[string]bool)+ expanded := make(map[string]bool)+ origins := make(map[string]WikiNode)+ var shortcuts []WikiNode+ for _, node := range nodes {+ if node.SpaceID == "" {+ node.SpaceID = spaceID+ }+ key := node.SpaceID + ":" + node.NodeToken+ seen[key] = true+ origins[key] = node+ if node.NodeType == "shortcut" && !node.HasChild {+ shortcuts = append(shortcuts, node)+ } else {+ processed[key] = true+ // Failed listings remain partial failures rather than being retried+ // through another alias during the same sync.+ if node.HasChild {+ expanded[key] = true+ }+ }+ }++ var walk func(string, []WikiNode) error+ walk = func(parentSpace string, children []WikiNode) error {+ for _, node := range children {+ if err := ctx.Err(); err != nil {+ return err+ }+ if node.SpaceID == "" {+ node.SpaceID = parentSpace+ }+ key := node.SpaceID + ":" + node.NodeToken+ if processed[key] {+ continue+ }+ processed[key] = true+ if !seen[key] {+ seen[key] = true+ nodes = append(nodes, node)+ }++ target, err := c.resolveWikiShortcut(ctx, node, origins)+ if err == nil && target.HasChild {+ key = target.SpaceID + ":" + target.NodeToken+ if expanded[key] {+ continue+ }+ expanded[key] = true+ var descendants []WikiNode+ descendants, err = c.ListWikiNodes(ctx, target.SpaceID, target.NodeToken)+ if err == nil {+ if node.NodeType == "shortcut" {+ // Keep the discovered subtree under the selected shortcut+ // for directory mapping, while retaining source space IDs.+ for i := range descendants {+ descendants[i].ParentNodeID = node.NodeToken+ }+ }+ if err := walk(target.SpaceID, descendants); err != nil {+ return err+ }+ }+ }+ if err != nil {+ if ctx.Err() != nil {+ return ctx.Err()+ }+ wrapped := fmt.Errorf("list children of %s: %w", node.NodeToken, err)+ failures = append(failures, WikiNodeListFailure{Node: node, Err: wrapped})+ logger.Warnf(ctx, "[Feishu] partial wiki node listing failure: space=%s node=%s err=%v",+ node.SpaceID, node.NodeToken, wrapped)+ }+ }+ return nil+ }+ if err := walk(spaceID, shortcuts); err != nil {+ return nodes, err+ }+ if len(failures) > 0 {+ return nodes, &PartialWikiNodeListError{Failures: failures}+ }+ return nodes, nil+}++func (c *Client) resolveWikiShortcut(ctx context.Context, node WikiNode, cache map[string]WikiNode) (WikiNode, error) {+ visited := make(map[string]bool)+ for node.NodeType == "shortcut" {+ key := node.SpaceID + ":" + node.NodeToken+ if visited[key] {+ return WikiNode{}, fmt.Errorf("wiki shortcut cycle at %s", node.NodeToken)+ }+ visited[key] = true+ if node.OriginNodeID == "" || node.OriginSpaceID == "" {+ return WikiNode{}, fmt.Errorf("wiki shortcut %s is missing its original node or space", node.NodeToken)+ }+ key = node.OriginSpaceID + ":" + node.OriginNodeID+ original, ok := cache[key]+ if !ok {+ var err error+ original, err = c.GetWikiNode(ctx, node.OriginSpaceID, node.OriginNodeID)+ if err != nil {+ return WikiNode{}, fmt.Errorf("resolve wiki shortcut %s: %w", node.NodeToken, err)+ }+ cache[key] = original+ }+ node = original+ }+ return node, nil+}diff --git a/internal/datasource/connector/feishu/wiki/connector.go b/internal/datasource/connector/feishu/wiki/connector.goindex c3fec3921..90a2ab1a0 100644--- a/internal/datasource/connector/feishu/wiki/connector.go+++ b/internal/datasource/connector/feishu/wiki/connector.go@@ -284,7 +284,7 @@ func (o *wikiOps) spaceDirPrefix(ctx context.Context, client *core.Client, spacefunc (o *wikiOps) List(ctx context.Context, client *core.Client, resourceID string) ([]core.WikiNode, error, error) {spaceID, nodeToken := parseWikiResourceID(resourceID)- nodes, err := client.ListWikiNodesRecursiveFrom(ctx, spaceID, nodeToken)+ nodes, err := client.ListWikiNodesWithShortcuts(ctx, spaceID, nodeToken)if err != nil {var partial *core.PartialWikiNodeListErrorif !errors.As(err, &partial) {@@ -344,6 +344,9 @@ func (o *wikiOps) Fetch(ctx context.Context, client *core.Client, n core.WikiNode, resourceID string,) ([]*types.FetchedItem, error) {spaceID, _ := parseWikiResourceID(resourceID)+ if n.SpaceID != "" {+ spaceID = n.SpaceID+ }items, err := fetchNodeContent(ctx, client, n, spaceID, resourceID, o.region, o.parseMode)if err != nil {return nil, err@@ -387,6 +390,10 @@ func (o *wikiOps) EncodeCursor(times map[string]map[string]string, lastSync timefunc appendWikiNodeListFailureItems(items []types.FetchedItem, spaceID string, resourceID string, failures []core.WikiNodeListFailure) []types.FetchedItem {for _, failure := range failures {node := failure.Node+ nodeSpaceID := node.SpaceID+ if nodeSpaceID == "" {+ nodeSpaceID = spaceID+ }title := node.Titleif title == "" {title = node.NodeToken@@ -398,7 +405,7 @@ func appendWikiNodeListFailureItems(items []types.FetchedItem, spaceID string, rMetadata: core.FeishuErrorItemMeta(failure.Err, map[string]string{"channel": types.ChannelFeishu,"node_token": node.NodeToken,- "space_id": spaceID,+ "space_id": nodeSpaceID,"failure_stage": "list_children",}),})