mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 10:58:17 -05:00
[PR #20966] feat: Smart sync for knowledge base directory uploads #48902
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/20966
Author: @Classic298
Created: 1/27/2026
Status: 🔄 Open
Base:
dev← Head:sync📝 Commits (10+)
6b2c927feat: Smart sync for knowledge base directory uploads (#146)4c58134fixes1cfd0fbfix form for upload file handler689f56bfix brittle form check for code QL0c61f0bfix: use size-based fallback for legacy files to prevent sync timeouta22aaecfix: add separate original_path field for directory syncd68d08bfix: sync compare syntax, replace scoping, and API error surfacinga7cdc4dfix: await process_file, tighten replace validation, handle dup paths and network errors86e6e23fix: close storage leaks, surface per-file sync failures, drop unused imports74163cefix: purge orphan KB vectors, route sync deletes through file delete, reject duplicate sync paths📊 Changes
7 files changed (+1087 additions, -191 deletions)
View changed files
📝
backend/open_webui/routers/files.py(+18 -3)📝
backend/open_webui/routers/knowledge.py(+477 -14)📝
backend/open_webui/routers/retrieval.py(+0 -1)📝
backend/open_webui/utils/misc.py(+5 -0)📝
src/lib/apis/knowledge/index.ts(+103 -0)📝
src/lib/components/workspace/Knowledge/KnowledgeBase.svelte(+483 -172)📝
src/lib/components/workspace/Knowledge/KnowledgeBase/AddContentMenu.svelte(+1 -1)📄 Description
Co-authored-by: Stoyan Zlatev 47269030+Stoyan-Zlatev@users.noreply.github.com
Summary
Refactors the "Sync Directory" feature for knowledge bases to intelligently compare files instead of deleting everything and re-uploading. Previously, syncing a directory would reset the entire knowledge base and re-upload all files - expensive and slow for large knowledge bases where only a few files changed.
What Changed
Smart Comparison
When syncing a directory, the browser now calculates SHA-256 hashes for all local files and sends them to a new comparison endpoint. The server compares these against existing files in the knowledge base and returns exactly what needs to happen:
Atomic File Replacement
For changed files, a new atomic endpoint handles the entire replacement in a single server-side transaction:
The old file is only deleted after the new file is fully processed and embedded. If any step fails, the old file remains intact.
Backwards Compatibility
Files uploaded before this feature don't have stored hashes. For these legacy files:
This avoids expensive I/O operations (downloading from storage + hashing) during sync comparison, preventing timeouts for large knowledge bases. When legacy files are re-uploaded (either as new or changed), they receive stored hashes for accurate future comparisons.
Why This Approach
Interrupted Sync Safety
Each file operation is atomic. If the browser disconnects mid-sync:
Performance
User Experience
Endpoints Added
POST /knowledge/{id}/sync/compare- Compare file hashes, return sync planPOST /knowledge/{id}/file/upload_and_replace- Atomic file replacementContributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.