An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Fix imported database relation search https://github.com/siyuan-note/siyuan/issues/18250
Daniel41b20475c44b
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.tsindex dc3094071b..dbbfd2a0ad 100644--- a/app/src/protyle/render/av/action.ts+++ b/app/src/protyle/render/av/action.ts@@ -644,7 +644,7 @@ export const avContextmenu = (protyle: IProtyle, rowElement: HTMLElement, positisrcIDs: sourceIds,avID,}]);- });+ }, true, blockElement.getAttribute("data-node-id"));}});if (rowElements.length === 1) {diff --git a/app/src/protyle/render/av/openMenuPanel.ts b/app/src/protyle/render/av/openMenuPanel.tsindex 13300b5758..42f1b72c33 100644--- a/app/src/protyle/render/av/openMenuPanel.ts+++ b/app/src/protyle/render/av/openMenuPanel.ts@@ -1301,7 +1301,7 @@ export const openMenuPanel = (options: {event.stopPropagation();break;} else if (type === "goSearchAV") {- openSearchAV(avID, target, undefined, false);+ openSearchAV(avID, target, undefined, false, options.blockElement.getAttribute("data-node-id"));event.preventDefault();event.stopPropagation();break;diff --git a/app/src/protyle/render/av/relation.ts b/app/src/protyle/render/av/relation.tsindex 6544cb68c5..b24ac98303 100644--- a/app/src/protyle/render/av/relation.ts+++ b/app/src/protyle/render/av/relation.ts@@ -26,9 +26,11 @@ interface IAVItem {viewLayout: string;}-const genSearchList = (element: Element, keyword: string, avId?: string, excludes = true, cb?: () => void) => {+const genSearchList = (element: Element, keyword: string, avId?: string, excludes = true, blockID?: string, cb?: () => void) => {fetchPost("/api/av/searchAttributeView", {keyword,+ avID: avId,+ blockID,excludes: (excludes && avId) ? [avId] : undefined}, (response) => {let html = "";@@ -76,7 +78,7 @@ const setDatabase = (avId: string, element: HTMLElement, item: HTMLElement) => {}};-export const openSearchAV = (avId: string, target: HTMLElement, cb?: (element: HTMLElement) => void, excludes = true) => {+export const openSearchAV = (avId: string, target: HTMLElement, cb?: (element: HTMLElement) => void, excludes = true, blockID?: string) => {window.siyuan.menus.menu.remove();const menu = new Menu();menu.addItem({@@ -114,10 +116,10 @@ export const openSearchAV = (avId: string, target: HTMLElement, cb?: (element: Hif (event.isComposing) {return;}- genSearchList(listElement, inputElement.value, avId, excludes);+ genSearchList(listElement, inputElement.value, avId, excludes, blockID);});inputElement.addEventListener("compositionend", () => {- genSearchList(listElement, inputElement.value, avId, excludes);+ genSearchList(listElement, inputElement.value, avId, excludes, blockID);});element.lastElementChild.addEventListener("click", (event) => {let clickTarget = event.target as HTMLElement;@@ -147,7 +149,7 @@ export const openSearchAV = (avId: string, target: HTMLElement, cb?: (element: HclickTarget = clickTarget.parentElement;}});- genSearchList(listElement, "", avId, excludes, () => {+ genSearchList(listElement, "", avId, excludes, blockID, () => {const rect = target.getBoundingClientRect();menu.open({x: rect.left,diff --git a/kernel/api/av.go b/kernel/api/av.goindex d0408f2831..4193946cdc 100644--- a/kernel/api/av.go+++ b/kernel/api/av.go@@ -739,13 +739,21 @@ func searchAttributeView(c *gin.Context) {}keyword := arg["keyword"].(string)+ currentAvID := ""+ if nil != arg["avID"] {+ currentAvID = arg["avID"].(string)+ }+ currentBlockID := ""+ if nil != arg["blockID"] {+ currentBlockID = arg["blockID"].(string)+ }var excludes []stringif nil != arg["excludes"] {for _, e := range arg["excludes"].([]any) {excludes = append(excludes, e.(string))}}- results := model.SearchAttributeView(keyword, excludes)+ results := model.SearchAttributeView(keyword, excludes, currentAvID, currentBlockID)ret.Data = map[string]any{"results": results,}diff --git a/kernel/av/av.go b/kernel/av/av.goindex 549117cc8e..ff168f9954 100644--- a/kernel/av/av.go+++ b/kernel/av/av.go@@ -467,6 +467,15 @@ func GetAttributeViewNameByPath(avJSONPath string) (ret string, err error) {return getAttributeViewNameByPathInBox(avJSONPath, "")}+// GetAttributeViewNameInBox 获取指定笔记本中的数据库名称。+func GetAttributeViewNameInBox(avID, boxID string) (ret string, err error) {+ avJSONPath, _ := FindAttributeViewPathInBox(avID, boxID)+ if avJSONPath == "" {+ return+ }+ return getAttributeViewNameByPathInBox(avJSONPath, boxID)+}+func GetAttributeViewContent(avID string) (content string) {if "" == avID {returndiff --git a/kernel/cli/cmd/database.go b/kernel/cli/cmd/database.goindex 8e103e5f9b..b8f6e89061 100644--- a/kernel/cli/cmd/database.go+++ b/kernel/cli/cmd/database.go@@ -40,7 +40,7 @@ var databaseSearchCmd = &cobra.Command{Short: "Search databases by name",Args: cobra.MinimumNArgs(1),RunE: func(cmd *cobra.Command, args []string) error {- results := model.SearchAttributeView(args[0], nil)+ results := model.SearchAttributeView(args[0], nil, "", "")switch outputFormat {case "json":data, _ := json.MarshalIndent(results, "", " ")diff --git a/kernel/mcp/tools/database.go b/kernel/mcp/tools/database.goindex a5fb4ab39a..3f4c28c409 100644--- a/kernel/mcp/tools/database.go+++ b/kernel/mcp/tools/database.go@@ -102,7 +102,7 @@ func databaseSearch(args map[string]any) (CallToolResult, error) {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "keyword is required"}}, IsError: true}, nil}- results := model.SearchAttributeView(keyword, nil)+ results := model.SearchAttributeView(keyword, nil, "", "")if len(results) == 0 {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no attribute views found"}}}, nil}diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.goindex 698eeeba81..b1f742bac3 100644--- a/kernel/model/attribute_view.go+++ b/kernel/model/attribute_view.go@@ -1911,7 +1911,7 @@ type AvSearchTempResult struct {Score float64}-func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearchResult) {+func SearchAttributeView(keyword string, excludeAvIDs []string, currentAvID, currentBlockID string) (ret []*AvSearchResult) {waitForSyncingStorages()ret = []*AvSearchResult{}@@ -1919,8 +1919,18 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearchkeywords := strings.Fields(keyword)var avSearchTmpResults []*AvSearchTempResult- // 只扫全局 AV 目录。加密笔记本的 AV 不参与全局搜索——避免跨加密边界暴露数据库名等元信息+ boxID := ""+ if currentBlockID != "" {+ if bt := treenode.GetBlockTree(currentBlockID); nil != bt && IsEncryptedBox(bt.BoxID) {+ boxID = bt.BoxID+ }+ } else if currentAvID != "" {+ _, boxID = av.FindAttributeViewPath(currentAvID)+ }avDir := filepath.Join(util.DataDir, "storage", "av")+ if boxID != "" {+ avDir = filepath.Join(util.DataDir, boxID, "storage", "av")+ }entries, err := os.ReadDir(avDir)if err != nil {logging.LogErrorf("read directory [%s] failed: %s", avDir, err)@@ -1950,7 +1960,7 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearchcontinue}- name, _ := av.GetAttributeViewNameByPath(filepath.Join(avDir, entry.Name()))+ name, _ := av.GetAttributeViewNameInBox(id, boxID)info, _ := entry.Info()if "" != keyword {score := 0.0@@ -1999,7 +2009,7 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearchbIDs := avBlockRels[tmpResult.AvID]var node *ast.Nodefor _, bID := range bIDs {- tree, _ := LoadTreeByBlockID(bID)+ tree, _ := loadTreeByBlockIDInBox(bID, boxID)if nil == tree {continue}@@ -2017,7 +2027,7 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearchcontinue}- attrView, _ := av.ParseAttributeView(tmpResult.AvID)+ attrView, _ := av.ParseAttributeViewInBox(tmpResult.AvID, boxID)if nil == attrView {continue}diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.goindex 2121a889b9..925b7bfdd3 100644--- a/kernel/model/attribute_view_render.go+++ b/kernel/model/attribute_view_render.go@@ -66,8 +66,12 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize intexistPath, _ = av.FindAttributeViewPath(avID)}if "" == existPath {- // fallback 找不到时按全局路径检查(首次创建场景)- existPath = av.GetAttributeViewDataPath(avID)+ if avBoxID != "" {+ existPath = filepath.Join(util.DataDir, avBoxID, "storage", "av", avID+".json")+ } else {+ // fallback 找不到时按全局路径检查(首次创建场景)+ existPath = av.GetAttributeViewDataPath(avID)+ }}if !filelock.IsExist(existPath) {if !createIfNotExist {@@ -90,6 +94,9 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize intlogging.LogErrorf("save attribute view [%s] failed: %s", avID, err)return}+ if blockID != "" {+ av.UpsertBlockRel(avID, blockID)+ }}// 已知 box 时直接用 InBox 解析,不依赖全局 pending 状态diff --git a/kernel/model/import.go b/kernel/model/import.goindex ab6326e898..1c2a1c0144 100644--- a/kernel/model/import.go+++ b/kernel/model/import.go@@ -343,9 +343,6 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {return ast.WalkContinue})- // 关联数据库和块- avNodes := tree.Root.ChildrenByType(ast.NodeAttributeView)- av.BatchUpsertBlockRel(avNodes)}// 如果数据库中绑定的块不在导入的文档中,则需要单独更新这些绑定块的属性@@ -739,6 +736,10 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {}boxAbsPath := filepath.Join(util.DataDir, boxID)+ importedAvIDs := map[string]struct{}{}+ for _, importedAvID := range avIDs {+ importedAvIDs[importedAvID] = struct{}{}+ }for _, treePath := range treePaths {absPath := filepath.Join(targetDir, treePath)p := strings.TrimPrefix(absPath, boxAbsPath)@@ -756,6 +757,13 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {}treenode.IndexBlockTree(tree)+ var avNodes []*ast.Node+ for _, avNode := range tree.Root.ChildrenByType(ast.NodeAttributeView) {+ if _, ok := importedAvIDs[avNode.AttributeViewID]; ok {+ avNodes = append(avNodes, avNode)+ }+ }+ av.BatchUpsertBlockRel(avNodes)sql.IndexTreeQueue(tree)util.PushEndlessProgress(Conf.language(73) + " " + fmt.Sprintf(Conf.language(70), tree.Root.IALAttr("title")))}