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: https://github.com/siyuan-note/siyuan/issues/18092
Danielb0d932cc018d
diff --git a/kernel/model/shortcuts.go b/kernel/model/shortcuts.goindex 80a5b16c3a..fe4614207f 100644--- a/kernel/model/shortcuts.go+++ b/kernel/model/shortcuts.go@@ -18,7 +18,9 @@ package modelimport ("bytes"+ "errors""os"+ "path""path/filepath""slices""strconv"@@ -106,9 +108,13 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {logging.LogErrorf("parse [%s] to int failed: %s", t, parseErr)continue}- hPath = "/" + time.UnixMilli(i).Format("2006-01-02 15:04:05")+ created := time.UnixMilli(i)+ hPath = "/" + created.Format("2006-01-02 15:04:05")+ // 块 ID 用速记输入时刻,而非 kernel 消费时刻+ dom := shorthandDOM(content, created)+ docID := util.NodeIDByTime(created)var retID string- retID, err = CreateWithMarkdown("", boxID, hPath, content, "", "", false, "", nil)+ retID, err = createShorthandDocByDOM(boxID, hPath, dom, docID)if nil != err {logging.LogErrorf("create doc failed: %s", err)return@@ -122,7 +128,11 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {hPath = "/" + hPath}- buff := bytes.Buffer{}+ type shorthand struct {+ content string+ created time.Time+ }+ var shorthands []shorthandfor _, entry := range entries {if filepath.Ext(entry.Name()) != ".md" {continue@@ -141,16 +151,38 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {continue}- buff.WriteString(content)- buff.WriteString("\n\n")+ t := strings.TrimSuffix(entry.Name(), ".md")+ i, parseErr := strconv.ParseInt(t, 10, 64)+ var created time.Time+ if nil != parseErr {+ // 文件名不是时间戳时退化为消费时刻,避免丢失速记内容+ created = time.Now()+ } else {+ created = time.UnixMilli(i)+ }+ shorthands = append(shorthands, shorthand{content: content, created: created})toRemoves = append(toRemoves, p)}- if 0 < buff.Len() {+ if 0 < len(shorthands) {bt := treenode.GetBlockTreeRootByHPath(boxID, hPath)if nil == bt {+ // 目标文档不存在,新建文档:根文档块 ID 取所有速记中最早的输入时刻+ earliest := shorthands[0].created+ for _, s := range shorthands[1:] {+ if s.created.Before(earliest) {+ earliest = s.created+ }+ }+ buff := bytes.Buffer{}+ for _, s := range shorthands {+ buff.WriteString(s.content)+ buff.WriteString("\n\n")+ }+ dom := shorthandDOM(buff.String(), earliest)+ docID := util.NodeIDByTime(earliest)var retID string- retID, err = CreateWithMarkdown("", boxID, hPath, buff.String(), "", "", false, "", nil)+ retID, err = createShorthandDocByDOM(boxID, hPath, dom, docID)if nil != err {logging.LogErrorf("create doc failed: %s", err)return@@ -168,18 +200,22 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {last = c}+ // 按条独立解析,每条速记的块 ID 用其各自的输入时刻luteEngine := util.NewStdLute()- inputTree := parse.Parse("", buff.Bytes(), luteEngine.ParseOptions)-- if nil != inputTree {- var nodes []*ast.Node+ var nodes []*ast.Node+ for _, s := range shorthands {+ inputTree := parse.Parse("", []byte(s.content), luteEngine.ParseOptions)+ if nil == inputTree {+ continue+ }for c := inputTree.Root.FirstChild; nil != c; c = c.Next {+ resetBlockIDsByTime(c, s.created)nodes = append(nodes, c)}- slices.Reverse(nodes)- for _, node := range nodes {- last.InsertAfter(node)- }+ }+ slices.Reverse(nodes)+ for _, node := range nodes {+ last.InsertAfter(node)}indexWriteTreeIndexQueue(tree)@@ -203,6 +239,67 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {return}+// resetBlockIDsByTime 递归地将节点及其子孙块的 ID 重置为基于指定时间。+// 无论节点原本是否有 ID 都会主动分配,以兼容上游 lute 未开启 KramdownBlockIAL 的解析路径。+// 重赋 ID 后同步更新 IAL 中的 id 和 updated(updated 取 ID 前 14 位,对齐 createDoc 新建块的处理)。+func resetBlockIDsByTime(node *ast.Node, created time.Time) {+ if nil == node {+ return+ }+ ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {+ if !entering || !n.IsBlock() || ast.NodeKramdownBlockIAL == n.Type {+ return ast.WalkContinue+ }+ n.ID = util.NodeIDByTime(created)+ n.SetIALAttr("id", n.ID)+ n.SetIALAttr("updated", util.TimeFromID(n.ID))+ return ast.WalkContinue+ })+}++// shorthandDOM 将速记 markdown 解析为 DOM,并把所有块 ID 替换为基于速记输入时刻。+func shorthandDOM(md string, created time.Time) string {+ luteEngine := util.NewLute()+ luteEngine.SetHTMLTag2TextMark(true)+ _, tree := luteEngine.Md2BlockDOMTree(md, false)+ if nil == tree {+ return ""+ }+ resetBlockIDsByTime(tree.Root, created)+ return luteEngine.Tree2BlockDOM(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)+}++// createShorthandDocByDOM 创建速记文档,使用指定的 DOM 和文档块 ID(均基于速记输入时刻)。+// 速记场景无需 tags、父文档、数学公式、剪藏链接等处理,直接基于 DOM 和指定 docID 落盘。+func createShorthandDocByDOM(boxID, hPath, dom, docID string) (retID string, err error) {+ createDocLock.Lock()+ defer createDocLock.Unlock()++ box := Conf.Box(boxID)+ if nil == box {+ err = errors.New(Conf.Language(0))+ return+ }++ FlushTxQueue()+ retID, err = createDocsByHPath(box.ID, hPath, dom, "", docID, false)+ if nil != err {+ return+ }+ FlushTxQueue()++ bt := treenode.GetBlockTree(retID)+ if nil == bt {+ logging.LogWarnf("get block tree by id [%s] failed after create", retID)+ return+ }+ box.setSortByConf(path.Dir(bt.Path), retID)++ FlushTxQueue()+ PushCreate(box, bt.Path, nil)+ return+}+var consumeShorthandsLock = sync.Mutex{}func consumeShorthands() {diff --git a/kernel/util/path.go b/kernel/util/path.goindex 1f90a947c6..fdfc639601 100644--- a/kernel/util/path.go+++ b/kernel/util/path.go@@ -126,6 +126,12 @@ func TimeFromID(id string) (ret string) {return}+// NodeIDByTime 根据指定时间生成符合块 ID 格式的字符串,算法与 ast.NewNodeID() 一致,+// 仅时间源不同:用于让历史输入(如移动端速记暂存文件名时间戳)回填为块 ID。+func NodeIDByTime(t time.Time) string {+ return t.Format("20060102150405") + "-" + RandString(7)+}+func GetChildDocDepth(treeAbsPath string) (ret int) {dir := strings.TrimSuffix(treeAbsPath, ".sy")if !gulu.File.IsDir(dir) {