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/4901
Liang Dinge5a17453e1f9
diff --git a/kernel/model/history.go b/kernel/model/history.goindex 265ed4fc02..29015c95a9 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -18,6 +18,7 @@ package modelimport ("encoding/json"+ "errors""fmt""io""io/fs"@@ -28,6 +29,7 @@ import ("time""github.com/88250/gulu"+ "github.com/88250/lute""github.com/88250/lute/parse""github.com/siyuan-note/filelock""github.com/siyuan-note/logging"@@ -48,6 +50,8 @@ func AutoGenerateDocHistory() {}func generateDocHistory() {+ defer logging.Recover()+if 1 > Conf.Editor.GenerateHistoryInterval {return}@@ -504,6 +508,9 @@ func (box *Box) generateDocHistory0() {return}}++ luteEngine := NewLute()+ indexHistoryDir(filepath.Base(historyDir), luteEngine)return}@@ -610,7 +617,6 @@ func indexHistory() {return}- validOps := []string{HistoryOpClean, HistoryOpUpdate, HistoryOpDelete, HistoryOpFormat}lutEngine := NewLute()for _, historyDir := range historyDirs {if !historyDir.IsDir() {@@ -618,77 +624,90 @@ func indexHistory() {}name := historyDir.Name()- op := name[strings.LastIndex(name, "-")+1:]- if !gulu.Str.Contains(op, validOps) {- logging.LogWarnf("invalid history op [%s]", op)- continue- }- t := name[:strings.LastIndex(name, "-")]- tt, parseErr := time.Parse("2006-01-02-150405", t)- if nil != parseErr {- logging.LogWarnf("parse time [%s] failed: %s", t, parseErr)- continue+ err = indexHistoryDir(name, lutEngine)+ if nil != err {+ return}- created := fmt.Sprintf("%d", tt.Unix())+ }+}- entryPath := filepath.Join(util.HistoryDir, name)- var docs, assets []string- filepath.Walk(entryPath, func(path string, info os.FileInfo, err error) error {- if strings.HasSuffix(info.Name(), ".sy") {- docs = append(docs, path)- } else if strings.Contains(path, "assets/") {- assets = append(assets, path)- }- return nil- })+var validOps = []string{HistoryOpClean, HistoryOpUpdate, HistoryOpDelete, HistoryOpFormat}- var histories []*sql.History- for _, doc := range docs {- tree, loadErr := loadTree(doc, lutEngine)- if nil != loadErr {- logging.LogErrorf("load tree [%s] failed: %s", doc, loadErr)- continue- }+func indexHistoryDir(name string, luteEngine *lute.Lute) (err error) {+ op := name[strings.LastIndex(name, "-")+1:]+ if !gulu.Str.Contains(op, validOps) {+ logging.LogWarnf("invalid history op [%s]", op)+ return+ }+ t := name[:strings.LastIndex(name, "-")]+ tt, parseErr := time.Parse("2006-01-02-150405", t)+ if nil != parseErr {+ logging.LogWarnf("parse history dir time [%s] failed: %s", t, parseErr)+ return+ }+ created := fmt.Sprintf("%d", tt.Unix())- title := tree.Root.IALAttr("title")- content := tree.Root.Content()- p := strings.TrimPrefix(doc, util.HistoryDir)- p = filepath.ToSlash(p[1:])- histories = append(histories, &sql.History{- Type: 0,- Op: op,- Title: title,- Content: content,- Path: p,- Created: created,- })+ entryPath := filepath.Join(util.HistoryDir, name)+ var docs, assets []string+ filepath.Walk(entryPath, func(path string, info os.FileInfo, err error) error {+ if strings.HasSuffix(info.Name(), ".sy") {+ docs = append(docs, path)+ } else if strings.Contains(path, "assets/") {+ assets = append(assets, path)}+ return nil+ })- for _, asset := range assets {- p := strings.TrimPrefix(asset, util.HistoryDir)- p = filepath.ToSlash(p[1:])- histories = append(histories, &sql.History{- Type: 1,- Op: op,- Title: filepath.Base(asset),- Path: p,- Created: created,- })+ var histories []*sql.History+ for _, doc := range docs {+ tree, loadErr := loadTree(doc, luteEngine)+ if nil != loadErr {+ logging.LogErrorf("load tree [%s] failed: %s", doc, loadErr)+ continue}- tx, txErr := sql.BeginHistoryTx()- if nil != txErr {- logging.LogErrorf("begin transaction failed: %s", txErr)- return- }- if err = sql.InsertHistories(tx, histories); nil != err {- logging.LogErrorf("insert histories failed: %s", err)- sql.RollbackTx(tx)- return- }- if err = sql.CommitTx(tx); nil != err {- logging.LogErrorf("commit transaction failed: %s", err)- return- }+ title := tree.Root.IALAttr("title")+ content := tree.Root.Content()+ p := strings.TrimPrefix(doc, util.HistoryDir)+ p = filepath.ToSlash(p[1:])+ histories = append(histories, &sql.History{+ Type: 0,+ Op: op,+ Title: title,+ Content: content,+ Path: p,+ Created: created,+ })}++ for _, asset := range assets {+ p := strings.TrimPrefix(asset, util.HistoryDir)+ p = filepath.ToSlash(p[1:])+ histories = append(histories, &sql.History{+ Type: 1,+ Op: op,+ Title: filepath.Base(asset),+ Path: p,+ Created: created,+ })+ }++ tx, txErr := sql.BeginHistoryTx()+ if nil != txErr {+ msg := fmt.Sprintf("begin transaction failed: %s", txErr)+ err = errors.New(msg)+ return+ }+ if err = sql.InsertHistories(tx, histories); nil != err {+ msg := fmt.Sprintf("insert histories failed: %s", err)+ err = errors.New(msg)+ sql.RollbackTx(tx)+ return+ }+ if err = sql.CommitTx(tx); nil != err {+ msg := fmt.Sprintf("commit transaction failed: %s", err)+ err = errors.New(msg)+ return+ }+ return}