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: Support short snapshot IDs and responsive search controls https://github.com/siyuan-note/siyuan/issues/19905
Daniel41c945ad41a2
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20210615213222-vs5tzbd.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20210615213222-vs5tzbd.syindex 4d0be85318..00dccf8e27 100644--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20210615213222-vs5tzbd.sy+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20210615213222-vs5tzbd.sy@@ -1599,7 +1599,7 @@"Children": [{"Type": "NodeText",- "Data": "To locate a local snapshot using an ID supplied by a plugin or another source, switch the search mode to Search snapshot ID, paste the full 40-character ID, and search. Matching is case-insensitive. The result keeps the usual snapshot actions, including rollback. Clear the search text to return to the full list, or switch back to file-name search to find files within snapshots."+ "Data": "To locate a local snapshot using an ID supplied by a plugin or another source, switch the search mode to Search snapshot ID, paste the full 40-character ID or a prefix of at least 7 characters, and search. Matching is case-insensitive. If multiple snapshots share the prefix, all matches are listed from newest to oldest. The result keeps the usual snapshot actions, including rollback. Clear the search text to return to the full list, or switch back to file-name search to find files within snapshots."}]},diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20210615211733-v6rzowm.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20210615211733-v6rzowm.syindex b60c08f454..980e9b4c70 100644--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20210615211733-v6rzowm.sy+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20210615211733-v6rzowm.sy@@ -1589,7 +1589,7 @@"Children": [{"Type": "NodeText",- "Data": "如需根据插件等来源提供的 ID 定位本地快照,可将搜索方式切换为按快照 ID 搜索,粘贴完整的 40 位 ID 后搜索,字母不区分大小写。结果保留回滚等常规快照操作。清空搜索内容可返回完整列表,也可切换回按文件名搜索以查找快照内的文件。"+ "Data": "如需根据插件等来源提供的 ID 定位本地快照,可将搜索方式切换为按快照 ID 搜索,粘贴完整的 40 位 ID 或至少 7 位前缀后搜索,字母不区分大小写。若前缀匹配多个快照,将按创建时间从新到旧列出全部结果。结果保留回滚等常规快照操作。清空搜索内容可返回完整列表,也可切换回按文件名搜索以查找快照内的文件。"}]},diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122707-8cr09co.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122707-8cr09co.syindex acc7b9036c..96f2d30fda 100644--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122707-8cr09co.sy+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122707-8cr09co.sy@@ -1575,7 +1575,7 @@"Children": [{"Type": "NodeText",- "Data": "如需依據外掛程式等來源提供的 ID 尋找本機快照,可將搜尋方式切換為依快照 ID 搜尋,貼上完整的 40 位 ID 後搜尋,字母不區分大小寫。結果保留回滾等一般快照操作。清空搜尋內容可返回完整清單,也可切換回依檔案名稱搜尋以尋找快照內的檔案。"+ "Data": "如需依據外掛程式等來源提供的 ID 尋找本機快照,可將搜尋方式切換為依快照 ID 搜尋,貼上完整的 40 位 ID 或至少 7 位前綴後搜尋,字母不區分大小寫。若前綴符合多個快照,將依建立時間由新到舊列出全部結果。結果保留回滾等一般快照操作。清空搜尋內容可返回完整清單,也可切換回依檔案名稱搜尋以尋找快照內的檔案。"}]},diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-txrc7z1.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-txrc7z1.syindex 9b546d86d8..c6d04d7168 100644--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-txrc7z1.sy+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-txrc7z1.sy@@ -1724,7 +1724,7 @@"Children": [{"Type": "NodeText",- "Data": "プラグインなどが提示した ID でローカルスナップショットを探すには、検索方法をスナップショット ID で検索に切り替え、40 桁の完全な ID を貼り付けて検索します。英字の大文字と小文字は区別されません。結果ではロールバックなどの通常の操作を利用できます。検索文字列を消すと一覧に戻ります。スナップショット内のファイルを探す場合は、ファイル名検索に切り替えてください。"+ "Data": "プラグインなどが提示した ID でローカルスナップショットを探すには、検索方法をスナップショット ID で検索に切り替え、40 桁の完全な ID または先頭の 7 桁以上を貼り付けて検索します。英字の大文字と小文字は区別されません。複数のスナップショットが一致する場合は、作成日時の新しい順にすべて表示されます。結果ではロールバックなどの通常の操作を利用できます。検索文字列を消すと一覧に戻ります。スナップショット内のファイルを探す場合は、ファイル名検索に切り替えてください。"}]},diff --git a/app/src/assets/scss/business/_history.scss b/app/src/assets/scss/business/_history.scssindex 92452fd50b..63a33c55f8 100644--- a/app/src/assets/scss/business/_history.scss+++ b/app/src/assets/scss/business/_history.scss@@ -248,6 +248,34 @@border-radius: 0 0 var(--b3-border-radius-b) var(--b3-border-radius-b);}+ &__repo-actions {+ flex-wrap: wrap;+ gap: 4px;+ }++ &__repo-search {+ display: flex;+ flex-wrap: wrap;+ align-items: center;+ gap: 4px;+ flex: 1 1 320px;+ min-width: 0;++ .b3-select {+ max-width: 100%;+ flex-shrink: 0;+ }++ .b3-form__icon {+ flex: 1 1 160px;+ min-width: 0;+ }++ .b3-text-field {+ width: 100%;+ }+ }+&__date {text-align: center;line-height: 20px;diff --git a/app/src/history/history.ts b/app/src/history/history.tsindex c40a90e424..e7133e2091 100644--- a/app/src/history/history.ts+++ b/app/src/history/history.ts@@ -404,6 +404,7 @@ const renderRepo = async (element: Element, currentPage: number) => {pageBtn.textContent = String(currentPage);searchInputElement.parentElement.classList.toggle("fn__none", selectValue !== "getRepoSnapshots");searchModeElement.classList.toggle("fn__none", selectValue !== "getRepoSnapshots");+ searchModeElement.parentElement.classList.toggle("fn__none", selectValue !== "getRepoSnapshots");searchButton.disabled = true;[previousElement, nextElement, pageBtn].forEach(button => {button.classList.toggle("fn__none", tagged);@@ -577,26 +578,26 @@ export const openHistory = (app: App, tab: "doc" | "notebook" | "repo" = "doc")</ul><div data-type="repo" class="fn__none history__repo"><div class="history__action">- <div class="block__icons">+ <div class="block__icons history__repo-actions">+ <div class="fn__flex fn__flex-center"><span data-type="previous" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href='#iconLeft'></use></svg></span><button class="b3-button b3-button--text ft__selectnone" data-type="jumpRepoPage" data-totalpage="1">1</button><span data-type="next" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href='#iconRight'></use></svg></span><span class="fn__space"></span><span class="ft__on-surface fn__flex-shrink ft__selectnone fn__none">${window.siyuan.languages.pageCountAndSnapshotCount}</span>- <span class="fn__space"></span>- <div class="fn__flex-1"></div>+ </div>+ <div class="history__repo-search fn__none"><select class="b3-select fn__none" data-type="repoSearchMode" aria-label="${window.siyuan.languages.search}"><option value="file">${window.siyuan.languages.searchFileName}</option><option value="id">${window.siyuan.languages.searchSnapshotID}</option></select><div class="b3-form__icon fn__none"><svg class="b3-form__icon-icon"><use xlink:href="#iconSearch"></use></svg>- <input class="b3-text-field b3-form__icon-input fn__size200" style="padding-right: 44px;" spellcheck="false" placeholder="${window.siyuan.languages.searchFileName}">+ <input class="b3-text-field b3-form__icon-input" style="padding-right: 44px;" spellcheck="false" placeholder="${window.siyuan.languages.searchFileName}"><button class="b3-button b3-button--text" style="position: absolute;right: 0;top: 0;">${window.siyuan.languages.search}</button></div>- <span class="fn__space"></span>+ </div><button class="b3-button b3-button--outline" disabled data-type="compare">${window.siyuan.languages.compare}</button>- <span class="fn__space"></span><button class="b3-button b3-button--outline" data-type="genRepo"><svg><use xlink:href="#iconAdd"></use></svg>${window.siyuan.languages.createSnapshot}</button>diff --git a/app/src/history/repoSearch.test.ts b/app/src/history/repoSearch.test.tsindex 591b8a57d1..79a589488f 100644--- a/app/src/history/repoSearch.test.ts+++ b/app/src/history/repoSearch.test.ts@@ -20,7 +20,7 @@ it("keeps snapshot ID lookup separate from file search and normal pagination", aconst next = {...node(), nextElementSibling: {nextElementSibling: node()}};const nodes: Record<string, unknown> = {".b3-text-field": searchInput,- '[data-type="repoSearchMode"]': {...node(), value: test.mode},+ '[data-type="repoSearchMode"]': {...node(), value: test.mode, parentElement: node()},'[data-type="repoList"]': node(),'button[data-type="jumpRepoPage"]': node(),'[data-type="previous"]': node(),diff --git a/kernel/api/contract_repo_test.go b/kernel/api/contract_repo_test.goindex 5f2a78a7d1..5f78ea63a0 100644--- a/kernel/api/contract_repo_test.go+++ b/kernel/api/contract_repo_test.go@@ -650,7 +650,7 @@ func TestRepoSnapshotIDContractHTTP(t *testing.T) {id stringcode intcount int- }{{" " + strings.ToUpper(id) + " ", 0, 1}, {strings.Repeat("0", 40), 0, 0}, {"../invalid", -1, 0}} {+ }{{" " + strings.ToUpper(id) + " ", 0, 1}, {id[:7], 0, 1}, {strings.Repeat("0", 40), 0, 0}, {"abcdef0", 0, 0}, {"../invalid", -1, 0}} {recorder := httptest.NewRecorder()engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, endpoint,strings.NewReader(fmt.Sprintf(`{"page":99,"id":%q}`, test.id))))diff --git a/kernel/apicontract/repo.go b/kernel/apicontract/repo.goindex 0c9276d520..a89d7535f5 100644--- a/kernel/apicontract/repo.go+++ b/kernel/apicontract/repo.go@@ -59,7 +59,8 @@ type UploadCloudSnapshotRequest struct {}type GetRepoSnapshotsRequest struct {Page float64 `json:"page"`- // ID 可选,去除首尾空白后按完整的 40 位十六进制快照 ID 查询本地仓库,忽略分页。+ // ID 可选,去除首尾空白后按 7 至 40 位十六进制快照 ID 前缀查询本地仓库,不区分大小写,忽略分页。+ // 前缀匹配多个快照时全部返回,按创建时间降序排列。// 省略或留空时保留分页列表;未找到返回空列表,格式错误及仓库读取失败返回错误。ID string `json:"id" api:"optional"`}diff --git a/kernel/model/repository.go b/kernel/model/repository.goindex 237c13457c..bcb6a6b9d6 100644--- a/kernel/model/repository.go+++ b/kernel/model/repository.go@@ -23,7 +23,6 @@ import ("crypto/sha1""crypto/sha256""encoding/base64"- "encoding/hex""errors""fmt""math"@@ -989,12 +988,11 @@ type TypeCount struct {Count int `json:"count"`}-// SearchRepoSnapshot 按完整 ID 读取本地快照,复用列表的元数据和下载状态计算。+// SearchRepoSnapshot 按完整 ID 或至少 7 位前缀读取本地快照,复用列表的元数据和下载状态计算。func SearchRepoSnapshot(id string) (ret []*Snapshot, pageCount, totalCount int, err error) {ret = []*Snapshot{}id = strings.ToLower(strings.TrimSpace(id))- decoded, decodeErr := hex.DecodeString(id)- if decodeErr != nil || len(decoded) != sha1.Size {+ if len(id) < 7 || len(id) > sha1.Size*2 || strings.Trim(id, "0123456789abcdef") != "" {return ret, 0, 0, errors.New("invalid snapshot ID")}if len(Conf.Repo.Key) == 0 {@@ -1004,25 +1002,51 @@ func SearchRepoSnapshot(id string) (ret []*Snapshot, pageCount, totalCount int,if err != nil {return}- index, err := repo.GetIndex(id)- if os.IsNotExist(err) || errors.Is(err, dejavu.ErrNotFoundIndex) {- return ret, 0, 0, nil+ ids := []string{id}+ if len(id) < sha1.Size*2 {+ entries, readErr := os.ReadDir(filepath.Join(repo.Path, "indexes"))+ if os.IsNotExist(readErr) {+ return ret, 0, 0, nil+ }+ if readErr != nil {+ return ret, 0, 0, readErr+ }+ ids = nil+ for _, entry := range entries {+ name := entry.Name()+ if !entry.IsDir() && len(name) == sha1.Size*2 && strings.HasPrefix(name, id) &&+ strings.Trim(name, "0123456789abcdef") == "" {+ ids = append(ids, name)+ }+ }}- if err != nil {- return+ var logs []*dejavu.Log+ for _, fullID := range ids {+ index, readErr := repo.GetIndex(fullID)+ if os.IsNotExist(readErr) || errors.Is(readErr, dejavu.ErrNotFoundIndex) {+ continue+ }+ if readErr != nil {+ return ret, 0, 0, readErr+ }+ files, readErr := repo.GetFiles(index)+ if readErr != nil {+ return ret, 0, 0, readErr+ }+ logs = append(logs, &dejavu.Log{+ ID: index.ID, Memo: index.Memo, Created: index.Created,+ HCreated: time.UnixMilli(index.Created).Format("2006-01-02 15:04:05"),+ Files: files, Count: index.Count, Size: index.Size,+ HSize: humanize.BytesCustomCeil(uint64(index.Size), 2),+ SystemID: index.SystemID, SystemName: index.SystemName, SystemOS: index.SystemOS,+ })}- files, err := repo.GetFiles(index)- if err != nil {- return+ if len(logs) == 0 {+ return ret, 0, 0, nil}- ret = buildSnapshots([]*dejavu.Log{{- ID: index.ID, Memo: index.Memo, Created: index.Created,- HCreated: time.UnixMilli(index.Created).Format("2006-01-02 15:04:05"),- Files: files, Count: index.Count, Size: index.Size,- HSize: humanize.BytesCustomCeil(uint64(index.Size), 2),- SystemID: index.SystemID, SystemName: index.SystemName, SystemOS: index.SystemOS,- }})- return ret, 1, 1, nil+ sort.SliceStable(logs, func(i, j int) bool { return logs[i].Created > logs[j].Created })+ ret = buildSnapshots(logs)+ return ret, 1, len(ret), nil}func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err error) {diff --git a/kernel/model/repository_snapshot_test.go b/kernel/model/repository_snapshot_test.goindex 69977b2a9a..fcbf0603dc 100644--- a/kernel/model/repository_snapshot_test.go+++ b/kernel/model/repository_snapshot_test.go@@ -22,7 +22,7 @@ func TestSearchRepoSnapshot(t *testing.T) {if err != nil {t.Fatal(err)}- for _, id := range []string{index.ID, " \n" + strings.ToUpper(index.ID) + "\t"} {+ for _, id := range []string{index.ID, index.ID[:7], index.ID[:15], " \n" + strings.ToUpper(index.ID[:7]) + "\t", " \n" + strings.ToUpper(index.ID) + "\t"} {found, pages, total, searchErr := SearchRepoSnapshot(id)if searchErr != nil || len(found) != 1 || pages != 1 || total != 1 {t.Fatalf("search %q: %v %d %d %v", id, found, pages, total, searchErr)@@ -43,7 +43,7 @@ func TestSearchRepoSnapshot(t *testing.T) {if found, pages, total, err := SearchRepoSnapshot(missing); err != nil || len(found) != 0 || pages != 0 || total != 0 {t.Fatalf("missing snapshot: %v %d %d %v", found, pages, total, err)}- for _, id := range []string{"", "short", "../../outside", strings.Repeat("z", 40)} {+ for _, id := range []string{"", "short", "abcdef", "abcdefg", "../../outside", strings.Repeat("z", 40), strings.Repeat("a", 41)} {if _, _, _, err := SearchRepoSnapshot(id); err == nil {t.Fatalf("invalid ID accepted: %q", id)}@@ -52,6 +52,16 @@ func TestSearchRepoSnapshot(t *testing.T) {if err != nil {t.Fatal(err)}+ other := *index+ other.ID = index.ID[:7] + strings.Repeat("0", 33)+ other.Created = index.Created + 1000+ if err = store.PutIndex(&other); err != nil {+ t.Fatal(err)+ }+ if found, pages, total, searchErr := SearchRepoSnapshot(index.ID[:7]); searchErr != nil || pages != 1 || total != 2 ||+ len(found) != 2 || found[0].ID != other.ID || found[1].ID != index.ID {+ t.Fatalf("shared prefix: %v %d %d %v", found, pages, total, searchErr)+ }corruptID := strings.Repeat("1", 40)dir, file := store.IndexAbsPath(corruptID)if err = os.MkdirAll(dir, 0755); err != nil {@@ -64,6 +74,9 @@ func TestSearchRepoSnapshot(t *testing.T) {if _, _, _, err = SearchRepoSnapshot(corruptID); err == nil {t.Fatal("corrupt snapshot returned as missing")}+ if _, _, _, err = SearchRepoSnapshot(corruptID[:7]); err == nil {+ t.Fatal("corrupt snapshot prefix returned as missing")+ }if unchanged, err := os.ReadFile(file); err != nil || !bytes.Equal(unchanged, corrupt) {t.Fatal("corrupt source was modified")}