An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve block op api
Danieldb266e7fabeb
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.goindex 486d466c52..53a7dac43d 100644--- a/kernel/agent/agent.go+++ b/kernel/agent/agent.go@@ -43,6 +43,9 @@ const systemPrompt = `You are a SiYuan AI assistant. You help users manage their## Domain Concepts- Block: the fundamental unit. Everything is a block with a unique ID, including documents (a document block, type NodeDocument, is the root). Content blocks (headings, paragraphs, lists, code, tables) form a tree under a document block.+- Container blocks (can hold child blocks): document, blockquote, list, list-item, super-block, callout. Leaf blocks (cannot hold children): heading, paragraph, code-block, math-block, table, HTML-block, thematic-break, video, audio, widget, iframe, attribute-view, block-query-embed.+- Heading hierarchy: headings (h1-h6) are leaf blocks. Blocks that appear "under" a heading in the UI are its *following siblings* in the AST, not its children. To place a block below a heading, pass the heading's ID (or the ID of the last block currently below it) as previousID, not as parentID.+- Nested lists: a list-item cannot directly contain another list-item. To nest lists, create a list (NodeList) as a child of the outer list-item, then add list-items to that inner list. The parent of a list-item must always be a list (NodeList).- Notebook: a top-level container holding documents. Use notebook.list to enumerate; pass notebook ID when creating documents.- hPath (human-readable path): the title-based path shown in the document tree, e.g. "/Diary/2024/June". The "path" parameter in document.create/move/list refers to hPath, not the internal ID-based filesystem path. A rename changes hPath but not the ID.- Document vs block move: document.move relocates an entire document (and children) to a new hPath within a notebook — needs id, notebook (from document.get field "Box"), and path. block.move repositions a single content block under a new parent block.diff --git a/kernel/api/block_op.go b/kernel/api/block_op.goindex b2f41fd55c..0170103e8c 100644--- a/kernel/api/block_op.go+++ b/kernel/api/block_op.go@@ -512,6 +512,20 @@ func moveBlock(c *gin.Context) {return}+ // 仅靠 parentID 定位目标时(无 previousID),目标必须是容器块,否则非法嵌套+ if "" == previousID && "" != parentID {+ if err := treenode.CheckListItemNesting(parentID, id); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+ }+transactions := []*model.Transaction{{DoOperations: []*model.Operation{@@ -549,6 +563,12 @@ func appendBlock(c *gin.Context) {if util.InvalidIDPattern(parentID, ret) {return}+ // append 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }if "markdown" == dataType {luteEngine := util.NewLute()var err error@@ -599,6 +619,12 @@ func batchAppendBlock(c *gin.Context) {if util.InvalidIDPattern(parentID, ret) {return}+ // append 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }if "markdown" == dataType {var err errordata, err = dataBlockDOM(data, luteEngine)@@ -642,6 +668,12 @@ func prependBlock(c *gin.Context) {if util.InvalidIDPattern(parentID, ret) {return}+ // prepend 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }if "markdown" == dataType {luteEngine := util.NewLute()var err error@@ -692,6 +724,12 @@ func batchPrependBlock(c *gin.Context) {if util.InvalidIDPattern(parentID, ret) {return}+ // prepend 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }if "markdown" == dataType {var err errordata, err = dataBlockDOM(data, luteEngine)@@ -751,6 +789,15 @@ func insertBlock(c *gin.Context) {}}+ // 仅靠 parentID 定位目标时(无 previousID/nextID),目标必须是容器块,否则非法嵌套+ if "" != parentID && "" == previousID && "" == nextID {+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+ }+if "markdown" == dataType {luteEngine := util.NewLute()var err error@@ -920,6 +967,15 @@ func batchInsertBlock(c *gin.Context) {}}+ // 仅靠 parentID 定位目标时(无 previousID/nextID),目标必须是容器块,否则非法嵌套+ if "" != parentID && "" == previousID && "" == nextID {+ if err := treenode.CheckContainerParent(parentID); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }+ }+if "markdown" == dataType {var err errordata, err = dataBlockDOM(data, luteEngine)diff --git a/kernel/cli/cmd/block.go b/kernel/cli/cmd/block.goindex 155f135f3b..88d2113f5f 100644--- a/kernel/cli/cmd/block.go+++ b/kernel/cli/cmd/block.go@@ -203,6 +203,13 @@ var blockInsertCmd = &cobra.Command{return nil}+ // 仅靠 parentID 定位目标时(无 previousID),目标必须是容器块,否则非法嵌套+ if previousID == "" {+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return err+ }+ }+data, err := resolveData(cmd)if err != nil {return err@@ -241,6 +248,11 @@ var blockAppendCmd = &cobra.Command{return nil}+ // append 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return err+ }+data, err := resolveData(cmd)if err != nil {return err@@ -278,6 +290,11 @@ var blockPrependCmd = &cobra.Command{return nil}+ // prepend 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return err+ }+data, err := resolveData(cmd)if err != nil {return err@@ -390,6 +407,16 @@ var blockMoveCmd = &cobra.Command{return nil}+ // 仅靠 parentID 定位目标时(无 previousID),目标必须是容器块,否则非法嵌套+ if previousID == "" {+ if err := treenode.CheckListItemNesting(parentID, id); err != nil {+ return err+ }+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return err+ }+ }+transactions := []*model.Transaction{{DoOperations: []*model.Operation{{Action: "move",diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.goindex 57b8b3dc81..97af11b0e0 100644--- a/kernel/mcp/tools/block.go+++ b/kernel/mcp/tools/block.go@@ -164,6 +164,13 @@ func blockInsert(args map[string]interface{}) (CallToolResult, error) {nextID = v}+ // 仅靠 parentID 定位目标时,目标必须是容器块,否则非法嵌套+ if parentID != "" && previousID == "" && nextID == "" {+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil+ }+ }+if dataType == "markdown" {var err errordata, err = markdownToBlockDOM(data)@@ -210,6 +217,10 @@ func blockAppend(args map[string]interface{}) (CallToolResult, error) {if parentID == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "parentID is required"}}, IsError: true}, nil}+ // append 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil+ }if dataType == "markdown" {var err error@@ -245,6 +256,10 @@ func blockPrepend(args map[string]interface{}) (CallToolResult, error) {if parentID == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "parentID is required"}}, IsError: true}, nil}+ // prepend 只用 parentID 定位目标,目标必须是容器块,否则非法嵌套+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil+ }if dataType == "markdown" {var err error@@ -360,6 +375,16 @@ func blockMove(args map[string]interface{}) (CallToolResult, error) {}previousID, _ := args["previousID"].(string)+ // 仅靠 parentID 定位目标时(无 previousID),目标必须是容器块,否则 doMove parent-only 分支会形成非法嵌套+ if previousID == "" {+ if err := treenode.CheckListItemNesting(parentID, id); err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil+ }+ if err := treenode.CheckContainerParent(parentID); err != nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: err.Error()}}, IsError: true}, nil+ }+ }+transactions := []*model.Transaction{{DoOperations: []*model.Operation{{Action: "move",diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.goindex bf0ffe3815..e913880d2d 100644--- a/kernel/sql/block_query.go+++ b/kernel/sql/block_query.go@@ -91,11 +91,7 @@ func QueryRootBlockByCondition(condition string, limit int) (ret []*Block) {}func (block *Block) IsContainerBlock() bool {- switch block.Type {- case "d", "b", "l", "i", "s":- return true- }- return false+ return treenode.IsContainerType(block.Type)}func queryBlockChildrenIDs(id string) (ret []string) {diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.goindex 1f34266cbc..28738294cc 100644--- a/kernel/treenode/blocktree.go+++ b/kernel/treenode/blocktree.go@@ -20,6 +20,7 @@ import ("bytes""database/sql""errors"+ "fmt""runtime""runtime/debug""strings"@@ -379,6 +380,51 @@ func GetBlockTree(id string) (ret *BlockTree) {return}+// IsContainerType 按主类型缩写判断块是否为容器块(可合法接收子块)。+// 入参 abbrType 对应 BlockTree.Type(如 "d"/"h"/"p"),由 TypeAbbr 写入。+func IsContainerType(abbrType string) bool {+ switch abbrType {+ case "d", "b", "l", "i", "s", "callout":+ return true+ }+ return false+}++// CheckContainerParent 校验 parentID 指向的块是否允许接收子块。+// 仅在“通过 parentID 定位插入/移动目标”(即不依赖 previousID/nextID)的场景下调用,+// 因为一旦带 previousID/nextID,事务层走的是兄弟级 InsertAfter/InsertBefore,天然合法。+// 返回 nil 表示合法;返回 error 时调用方应拒绝本次操作。+func CheckContainerParent(parentID string) error {+ bt := GetBlockTree(parentID)+ if nil == bt {+ return fmt.Errorf("parent block not found: %s", parentID)+ }+ if IsContainerType(bt.Type) {+ return nil+ }+ if "h" == bt.Type {+ // 标题是叶子块,其“子内容”在数据结构上实为后续兄弟节点(由 HeadingChildren 按层级推算)。+ // 把块挂成标题的 AST 子节点属于非法嵌套,应改用 previousID 定位。+ return fmt.Errorf("heading [%s] is a leaf block and cannot have children; to place a block below this heading, pass previousID=<heading id> or previousID=<last block below the heading> instead of parentID", parentID)+ }+ return fmt.Errorf("block [%s] type %q is a leaf block and cannot have children; use previousID to place the block as its sibling instead", parentID, bt.Type)+}++// CheckListItemNesting 校验 parentID 和 childID 是否形成“列表项直含列表项”的非法嵌套。+// 嵌套列表的正确结构是 ListItem > List > ListItem,列表项不能直接作为另一个列表项的子块。+// 仅在 move 场景调用(源和目标类型均已知)。+func CheckListItemNesting(parentID, childID string) error {+ parentBt := GetBlockTree(parentID)+ childBt := GetBlockTree(childID)+ if nil == parentBt || nil == childBt {+ return nil // 查不到就放行,不阻塞未知场景+ }+ if "i" == parentBt.Type && "i" == childBt.Type {+ return fmt.Errorf("a list-item cannot directly contain another list-item; to nest, first create a list (NodeList) under the outer list-item, then add the inner list-items to that list")+ }+ return nil+}+func SetBlockTreePath(tree *parse.Tree) {RemoveBlockTreesByRootID(tree.ID)IndexBlockTree(tree)