Files
syops/README.md
T
2026-04-27 17:03:56 +08:00

211 lines
5.8 KiB
Markdown

# 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 UI
- `backend/`: FastAPI API, RQ worker tasks, vendored `edkb_system`
- `project-governance/`: Codex skill governance, workflow docs, and audit snapshots
- `docker-compose.yml`: full deployment stack for `192.168.100.230`
## Local Run
1. Copy `.env.example` to `.env` and fill in `DIFY_API_KEY`.
2. Start services:
```bash
docker compose up --build -d
```
3. 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.
1. Copy `backend/backend.env.example` to `backend/.env`.
2. Choose a database mode in `backend/.env`:
```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`.
3. Keep `TASK_EXECUTION_MODE=sync` for local smoke tests.
4. You can now use the project-local Python runtime under `python/`.
5. Start the backend:
```bash
cd backend
python -m alembic upgrade head
python -m uvicorn app.main:app --host 127.0.0.1 --port 8000
```
6. Start the frontend in another terminal:
```bash
cd frontend
npm run dev -- --host 127.0.0.1 --port 5174
```
7. Open `http://127.0.0.1:5174`.
Convenience scripts in the project root:
```powershell
.\start-local-backend.ps1
.\start-local-frontend.ps1
.\start-local-dev.ps1
```
Notes:
- `start-local-backend.ps1` runs Alembic first, then starts FastAPI.
- `start-local-frontend.ps1` starts the Vite dev server.
- `start-local-dev.ps1` starts both in background and writes logs to `tmp_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:
```bash
docker compose --env-file .env up --build -d
```
Database selection is now configurable through `.env`:
```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_URL` has the highest priority.
- When running inside Docker, `localhost` points to the container itself. A host-level PostgreSQL service must use a reachable host name or IP instead of `localhost`.
Current deployment path on the server:
- `/home/inspur/tools/document-cleaning`
## Core Flows
- Upload one or many files into a group.
- Deduplicate by `SHA-256` within 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.
1. Set `INTEGRATION_API_KEY` in `.env`.
2. Optionally set `INTEGRATION_DOWNLOAD_SECRET` to a dedicated signing secret. If omitted, the app falls back to `JWT_SECRET`.
3. 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-bases`
- `GET /api/integration/files?knowledge_base_id=123`
- `GET /api/integration/files?file_name=report`
- `GET /api/integration/files/456`
- `GET /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:
```env
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 already` are 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:
```bash
cd backend
python -m pytest tests -q
```
Frontend:
```bash
cd frontend
npm install
npm run build
```