Document Cleaning
Independent internal web system for document cleaning, version tracking, batch download, and manual sync into Dify datasets.
Stack
- Frontend: Vue 3, Vite, Tailwind, Uppy
- Backend: FastAPI, SQLAlchemy, Alembic
- Queue: RQ + Redis
- Worker runtime: Python + LibreOffice
- Database: SQLite or PostgreSQL (configurable)
Project Layout
frontend/: Vue workspace UIbackend/: FastAPI API, RQ worker tasks, vendorededkb_systemproject-governance/: Codex skill governance, workflow docs, and audit snapshotsdocker-compose.yml: full deployment stack for192.168.100.230
Local Run
- Copy
.env.exampleto.envand fill inDIFY_API_KEY. - Start services:
docker compose up --build -d
- Open
http://localhost:8891.
Default login comes from .env:
- username:
admin - password:
admin123456
Local Direct Run Without Docker
Use this mode on a workstation that does not have Docker available.
- Copy
backend/backend.env.exampletobackend/.env. - Choose a database mode in
backend/.env:
# Option A: local SQLite
DATABASE_BACKEND=sqlite
DATABASE_URL=
SQLITE_DATABASE_URL=sqlite:///./document_cleaning.db
# Option B: local/shared PostgreSQL
DATABASE_BACKEND=postgres
DATABASE_URL=
POSTGRES_DATABASE_URL=postgresql+psycopg://postgres:difyai123456@localhost:5432/doccleaning_db
DATABASE_URL remains supported as a direct override. If it is set, it takes priority over DATABASE_BACKEND.
-
Keep
TASK_EXECUTION_MODE=syncfor local smoke tests. -
You can now use the project-local Python runtime under
python/. -
Start the backend:
cd backend
python -m alembic upgrade head
python -m uvicorn app.main:app --host 127.0.0.1 --port 8000
- Start the frontend in another terminal:
cd frontend
npm run dev -- --host 127.0.0.1 --port 5174
- Open
http://127.0.0.1:5174.
Convenience scripts in the project root:
.\start-local-backend.ps1
.\start-local-frontend.ps1
.\start-local-dev.ps1
Notes:
start-local-backend.ps1runs Alembic first, then starts FastAPI.start-local-frontend.ps1starts the Vite dev server.start-local-dev.ps1starts both in background and writes logs totmp_run/.
Notes:
- In local direct mode, queue tasks run inline through the API process.
- Real Dify sync still requires a valid
DIFY_API_KEY. - Server deployment should continue using
docker compose.
Production Deployment
The compose file is already configured for the planned entrypoint:
http://192.168.100.230:8891
Deploy on the target server with:
docker compose --env-file .env up --build -d
Database selection is now configurable through .env:
# Option A: use the compose PostgreSQL service
DATABASE_BACKEND=postgres
DATABASE_URL=
POSTGRES_DATABASE_URL=postgresql+psycopg://document_cleaning:document_cleaning@postgres:5432/document_cleaning
# Option B: use SQLite
DATABASE_BACKEND=sqlite
DATABASE_URL=
SQLITE_DATABASE_URL=sqlite:///./document_cleaning.db
# Option C: explicit override
DATABASE_URL=postgresql+psycopg://postgres:difyai123456@localhost:5432/doccleaning_db
Notes:
DATABASE_URLhas the highest priority.- When running inside Docker,
localhostpoints to the container itself. A host-level PostgreSQL service must use a reachable host name or IP instead oflocalhost.
Current deployment path on the server:
/home/inspur/tools/document-cleaning
Core Flows
- Upload one or many files into a group.
- Deduplicate by
SHA-256within the same group. - Create a new version when title matches but content changed.
- Convert in the background through RQ workers.
- Download original files or generated markdown individually or in bulk.
- Manually sync selected documents to Dify.
- Auto-create a Dify dataset on first sync for each group.
Dify Integration
- Group name maps to one Dify dataset.
- Sync uses cleaned markdown text, not the raw original file.
- Existing logical documents are updated in Dify instead of duplicated.
- Dataset defaults are managed in the admin console.
External Integration API
Use this when another internal site needs to query knowledge-base/file relationships without direct database access.
- Set
INTEGRATION_API_KEYin.env. - Optionally set
INTEGRATION_DOWNLOAD_SECRETto a dedicated signing secret. If omitted, the app falls back toJWT_SECRET. - Call the query API with
X-Integration-Key: <your key>.
Recommended pattern:
- External site backend stores
INTEGRATION_API_KEY - Backend calls this system's query API
- Query response returns short-lived signed download URLs
- External site frontend opens those signed URLs directly without carrying the integration key
Example endpoints:
GET /api/integration/knowledge-basesGET /api/integration/files?knowledge_base_id=123GET /api/integration/files?file_name=reportGET /api/integration/files/456GET /api/integration/dify-file?dataset_id=<dify_dataset_id>&remote_document_id=<dify_document_id>GET /api/integration/download?version_id=<local_version_id>&kind=source&token=<signed token>GET /api/integration/download?version_id=<local_version_id>&kind=md&token=<signed token>
Dify Sync Throttling
To avoid overloading Dify when many files are pushed at once, sync jobs now support retry backoff and a short global cooldown gate.
Relevant .env knobs:
DIFY_SYNC_MAX_RETRIES=4
DIFY_SYNC_RETRY_SCHEDULE=30,60,120,300
DIFY_SYNC_COOLDOWN_SECONDS=60
Behavior:
- Retryable Dify congestion errors such as
too many clients alreadyare not marked failed immediately. - The sync job is re-queued with backoff.
- A short global cooldown prevents the next sync jobs from hammering Dify during the same congestion window.
Tests
Backend:
cd backend
python -m pytest tests -q
Frontend:
cd frontend
npm install
npm run build