An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Command Line Interface https://github.com/siyuan-note/siyuan/issues/17674
Daniela50b15fbbd93
diff --git a/kernel/cli/cmd/attr.go b/kernel/cli/cmd/attr.gonew file mode 100644index 0000000000..0ddb8e2cc0--- /dev/null+++ b/kernel/cli/cmd/attr.go@@ -0,0 +1,149 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package cmd++import (+ "encoding/json"+ "fmt"+ "os"+ "sort"+ "strings"+ "text/tabwriter"++ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/sql"++ "github.com/spf13/cobra"+)++var attrCmd = &cobra.Command{+ Use: "attr",+ Short: "Manage block attributes",+}++var attrGetCmd = &cobra.Command{+ Use: "get --id <id>",+ Short: "Get block attributes",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }++ attrs := sql.GetBlockAttrs(id)+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(attrs, "", " ")+ fmt.Println(string(data))+ default:+ printAttrTable(attrs)+ }+ return nil+ },+}++var attrSetCmd = &cobra.Command{+ Use: "set --id <id> --attr name=value",+ Short: "Set block attributes",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }++ attrFlags, _ := cmd.Flags().GetStringArray("attr")+ if len(attrFlags) == 0 {+ return fmt.Errorf("--attr is required (format: name=value)")+ }++ nameValues := make(map[string]string, len(attrFlags))+ for _, a := range attrFlags {+ parts := strings.SplitN(a, "=", 2)+ if len(parts) != 2 {+ return fmt.Errorf("invalid attr format [%s], expected name=value", a)+ }+ nameValues[strings.TrimSpace(parts[0])] = parts[1]+ }++ if err := model.SetBlockAttrs(id, nameValues); err != nil {+ return err+ }+ fmt.Println("ok")+ return nil+ },+}++var attrBatchGetCmd = &cobra.Command{+ Use: "batch-get --ids id1,id2,...",+ Short: "Batch get block attributes",+ RunE: func(cmd *cobra.Command, args []string) error {+ idsStr, _ := cmd.Flags().GetString("ids")+ if idsStr == "" {+ return fmt.Errorf("--ids is required")+ }++ ids := strings.Split(idsStr, ",")+ for i := range ids {+ ids[i] = strings.TrimSpace(ids[i])+ }++ attrs := sql.BatchGetBlockAttrs(ids)+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(attrs, "", " ")+ fmt.Println(string(data))+ default:+ for id, a := range attrs {+ fmt.Printf("\n[%s]\n", id)+ printAttrTable(a)+ }+ }+ return nil+ },+}++func printAttrTable(attrs map[string]string) {+ if len(attrs) == 0 {+ fmt.Println("(empty)")+ return+ }++ keys := make([]string, 0, len(attrs))+ for k := range attrs {+ keys = append(keys, k)+ }+ sort.Strings(keys)++ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)+ fmt.Fprintln(w, "NAME\tVALUE")+ for _, k := range keys {+ fmt.Fprintf(w, "%s\t%s\n", k, attrs[k])+ }+ w.Flush()+}++func init() {+ attrGetCmd.Flags().String("id", "", "block ID")+ attrSetCmd.Flags().String("id", "", "block ID")+ attrSetCmd.Flags().StringArray("attr", nil, "attribute in name=value format (repeatable)")+ attrBatchGetCmd.Flags().String("ids", "", "comma-separated block IDs")++ rootCmd.AddCommand(attrCmd)+ attrCmd.AddCommand(attrGetCmd)+ attrCmd.AddCommand(attrSetCmd)+ attrCmd.AddCommand(attrBatchGetCmd)+}diff --git a/kernel/cli/cmd/block.go b/kernel/cli/cmd/block.gonew file mode 100644index 0000000000..8d24df61dc--- /dev/null+++ b/kernel/cli/cmd/block.go@@ -0,0 +1,384 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package cmd++import (+ "encoding/json"+ "fmt"+ "os"+ "text/tabwriter"++ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"++ "github.com/spf13/cobra"+)++var blockCmd = &cobra.Command{+ Use: "block",+ Short: "Block operations",+}++// ─── Read ──────────────────────────────────────────────────────────────────────++var blockGetCmd = &cobra.Command{+ Use: "get --id <id>",+ Short: "Get block info",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ block, err := model.GetBlock(id, nil)+ if err != nil {+ return err+ }+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(block, "", " ")+ fmt.Println(string(data))+ default:+ fmt.Printf("ID: %s\n", block.ID)+ fmt.Printf("Type: %s\n", block.Type)+ fmt.Printf("Name: %s\n", block.Name)+ fmt.Printf("Box: %s\n", block.Box)+ fmt.Printf("HPath: %s\n", block.HPath)+ if block.Content != "" {+ fmt.Printf("Content: %s\n", truncate(block.Content, 200))+ }+ if block.Markdown != "" {+ fmt.Printf("Markdown: %s\n", truncate(block.Markdown, 200))+ }+ }+ return nil+ },+}++var blockChildrenCmd = &cobra.Command{+ Use: "children --id <id>",+ Short: "Get child blocks",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ children := model.GetChildBlocks(id)+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(children, "", " ")+ fmt.Println(string(data))+ default:+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)+ fmt.Fprintln(w, "ID\tTYPE\tCONTENT")+ for _, c := range children {+ fmt.Fprintf(w, "%s\t%s\t%s\n", c.ID, c.Type, truncate(c.Content, 80))+ }+ w.Flush()+ }+ return nil+ },+}++var blockBreadcrumbCmd = &cobra.Command{+ Use: "breadcrumb --id <id>",+ Short: "Get block breadcrumb",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ paths, err := model.BuildBlockBreadcrumb(id, nil)+ if err != nil {+ return err+ }+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(paths, "", " ")+ fmt.Println(string(data))+ default:+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)+ fmt.Fprintln(w, "ID\tNAME\tTYPE")+ for _, p := range paths {+ fmt.Fprintf(w, "%s\t%s\t%s\n", p.ID, p.Name, p.Type)+ }+ w.Flush()+ }+ return nil+ },+}++var blockDomCmd = &cobra.Command{+ Use: "dom --id <id>",+ Short: "Get block DOM",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ fmt.Print(model.GetBlockDOM(id))+ return nil+ },+}++var blockKramdownCmd = &cobra.Command{+ Use: "kramdown --id <id>",+ Short: "Get block kramdown",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ mode, _ := cmd.Flags().GetString("mode")+ if mode == "" {+ mode = "md"+ }+ fmt.Print(model.GetBlockKramdown(id, mode))+ return nil+ },+}++var blockInfoCmd = &cobra.Command{+ Use: "info --id <id>",+ Short: "Get document info",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }+ info, err := model.GetDocInfo(id)+ if err != nil {+ return err+ }+ switch outputFormat {+ case "json":+ data, _ := json.MarshalIndent(info, "", " ")+ fmt.Println(string(data))+ default:+ fmt.Printf("ID: %s\n", info.ID)+ fmt.Printf("RootID: %s\n", info.RootID)+ fmt.Printf("Name: %s\n", info.Name)+ fmt.Printf("RefCount: %d\n", info.RefCount)+ fmt.Printf("SubFileCount: %d\n", info.SubFileCount)+ }+ return nil+ },+}++// ─── Write ─────────────────────────────────────────────────────────────────────++var blockInsertCmd = &cobra.Command{+ Use: "insert --parent <id> --data <markdown>",+ Short: "Insert block",+ RunE: func(cmd *cobra.Command, args []string) error {+ parentID, _ := cmd.Flags().GetString("parent")+ data, _ := cmd.Flags().GetString("data")+ previousID, _ := cmd.Flags().GetString("previous")+ if parentID == "" || data == "" {+ return fmt.Errorf("--parent and --data are required")+ }++ dom := markdownToBlockDOM(data)+ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "insert",+ Data: dom,+ ParentID: parentID,+ PreviousID: previousID,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println("ok")+ return nil+ },+}++var blockAppendCmd = &cobra.Command{+ Use: "append --parent <id> --data <markdown>",+ Short: "Append block",+ RunE: func(cmd *cobra.Command, args []string) error {+ parentID, _ := cmd.Flags().GetString("parent")+ data, _ := cmd.Flags().GetString("data")+ if parentID == "" || data == "" {+ return fmt.Errorf("--parent and --data are required")+ }++ dom := markdownToBlockDOM(data)+ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "append",+ Data: dom,+ ParentID: parentID,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println("ok")+ return nil+ },+}++var blockPrependCmd = &cobra.Command{+ Use: "prepend --parent <id> --data <markdown>",+ Short: "Prepend block",+ RunE: func(cmd *cobra.Command, args []string) error {+ parentID, _ := cmd.Flags().GetString("parent")+ data, _ := cmd.Flags().GetString("data")+ if parentID == "" || data == "" {+ return fmt.Errorf("--parent and --data are required")+ }++ dom := markdownToBlockDOM(data)+ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "prepend",+ Data: dom,+ ParentID: parentID,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println("ok")+ return nil+ },+}++var blockUpdateCmd = &cobra.Command{+ Use: "update --id <id> --data <markdown>",+ Short: "Update block",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ data, _ := cmd.Flags().GetString("data")+ if id == "" || data == "" {+ return fmt.Errorf("--id and --data are required")+ }++ dom := markdownToBlockDOM(data)+ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "update",+ Data: dom,+ ID: id,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println("ok")+ return nil+ },+}++var blockDeleteCmd = &cobra.Command{+ Use: "delete --id <id>",+ Short: "Delete block",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ if id == "" {+ return fmt.Errorf("--id is required")+ }++ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "delete",+ ID: id,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println(id)+ return nil+ },+}++var blockMoveCmd = &cobra.Command{+ Use: "move --id <id> --parent <id>",+ Short: "Move block",+ RunE: func(cmd *cobra.Command, args []string) error {+ id, _ := cmd.Flags().GetString("id")+ parentID, _ := cmd.Flags().GetString("parent")+ previousID, _ := cmd.Flags().GetString("previous")+ if id == "" || parentID == "" {+ return fmt.Errorf("--id and --parent are required")+ }++ transactions := []*model.Transaction{{+ DoOperations: []*model.Operation{{+ Action: "move",+ ID: id,+ ParentID: parentID,+ PreviousID: previousID,+ }},+ }}+ model.PerformTransactions(&transactions)+ model.FlushTxQueue()+ fmt.Println("ok")+ return nil+ },+}++func markdownToBlockDOM(md string) string {+ luteEngine := util.NewLute()+ luteEngine.SetHTMLTag2TextMark(true)+ dom, _ := luteEngine.Md2BlockDOMTree(md, true)+ if dom == "" {+ dom = "<div data-type=\"NodeParagraph\" data-node-id=\"\"><div data-type=\"NodeText\"></div></div>"+ }+ return dom+}++func init() {+ blockGetCmd.Flags().String("id", "", "block ID")+ blockChildrenCmd.Flags().String("id", "", "parent block ID")+ blockBreadcrumbCmd.Flags().String("id", "", "block ID")+ blockDomCmd.Flags().String("id", "", "block ID")+ blockKramdownCmd.Flags().String("id", "", "block ID")+ blockKramdownCmd.Flags().String("mode", "md", "export mode: md | textmark")+ blockInfoCmd.Flags().String("id", "", "document block ID")++ blockInsertCmd.Flags().String("parent", "", "parent block ID")+ blockInsertCmd.Flags().String("data", "", "markdown content")+ blockInsertCmd.Flags().String("previous", "", "previous sibling block ID")++ blockAppendCmd.Flags().String("parent", "", "parent block ID")+ blockAppendCmd.Flags().String("data", "", "markdown content")++ blockPrependCmd.Flags().String("parent", "", "parent block ID")+ blockPrependCmd.Flags().String("data", "", "markdown content")++ blockUpdateCmd.Flags().String("id", "", "block ID")+ blockUpdateCmd.Flags().String("data", "", "markdown content")++ blockDeleteCmd.Flags().String("id", "", "block ID")++ blockMoveCmd.Flags().String("id", "", "block ID")+ blockMoveCmd.Flags().String("parent", "", "target parent block ID")+ blockMoveCmd.Flags().String("previous", "", "target previous sibling block ID")++ rootCmd.AddCommand(blockCmd)+ blockCmd.AddCommand(blockGetCmd)+ blockCmd.AddCommand(blockChildrenCmd)+ blockCmd.AddCommand(blockBreadcrumbCmd)+ blockCmd.AddCommand(blockDomCmd)+ blockCmd.AddCommand(blockKramdownCmd)+ blockCmd.AddCommand(blockInfoCmd)+ blockCmd.AddCommand(blockInsertCmd)+ blockCmd.AddCommand(blockAppendCmd)+ blockCmd.AddCommand(blockPrependCmd)+ blockCmd.AddCommand(blockUpdateCmd)+ blockCmd.AddCommand(blockDeleteCmd)+ blockCmd.AddCommand(blockMoveCmd)+}