Initial commit: book-mcp 教辅处理 MCP server + Arco Vue 管理端

后端 (book_mcp_server.py, 335 行, 15 个 MCP 工具):
- create_book / add_document / mark_html_ready / mark_json_ready / mark_failed
- update_book_status / soft_delete_book / retry_failed
- get_book / list_books / get_document / list_documents / get_document_url
- upload_to_rustfs / health
- 走 JSON-RPC + stateless_http + json_response(前端可直接调)
- launchd 守护 / 6 秒拉起(macOS)
- systemd 守护脚本(Linux/codelab)

存储:
- MongoDB (mongo:8.0 容器) - book/book_document/document_file 三表
- RustFS 对象存储(fs.code-lab.cn/scan/)- book/<id>/docs/<doc_id>/{docx,html,json}
- 幂等设计: zip_sha256 唯一 + (book_id+original_path) 唯一

前端 (Vue 3 + TS + Arco Design):
- 列表页 /book/list - 搜索 + 状态过滤 + 进度
- 详情页 /book/:id - 文档折叠 + 文件下载
- MCP 客户端 src/api/mcp.ts (JSON-RPC over fetch)

部署:
- deploy/install.sh (codelab 一键装 backend + systemd)
- deploy/install-frontend.sh (vite dev / 生产构建)
- docs/deploy.md (运维手册)

测试:
- tests/test_basic.py (sanity 7 项)
- tests/test_e2e.py (完整 e2e: docx→HTML→入库→上传→校验)
This commit is contained in:
2026-10-11 18:03:45 +08:00
parent f7fb571bce
commit 9b0910ccb1
160 changed files with 23628 additions and 3 deletions

50
services/file_service.py Normal file
View File

@@ -0,0 +1,50 @@
"""文档文件 (document_file) 业务层 — MongoDB 版本
upsert 策略:(doc_id, file_type) 唯一索引 → update_one / insert_one
"""
from pymongo.errors import DuplicateKeyError
from db import get_collection, COL_FILE, next_id, DocumentFile
from db.models import _now
def upsert_file(doc_id: str, file_type: str, object_key: str, object_bucket: str,
size: int = 0, checksum: str | None = None, url: str | None = None,
meta: dict | None = None) -> dict:
"""插入或更新 (doc_id, file_type) 唯一键"""
col = get_collection(COL_FILE)
filt = {'doc_id': doc_id, 'file_type': file_type}
upd = {'$set': {
'object_key': object_key, 'object_bucket': object_bucket,
'size': size, 'checksum': checksum, 'url': url, 'meta': meta,
}}
r = col.update_one(filt, upd)
if r.matched_count > 0:
f = col.find_one(filt)
return {'id': f['_id'], 'updated': True}
f = DocumentFile(
id=next_id('file_id'),
doc_id=doc_id, file_type=file_type, object_key=object_key,
object_bucket=object_bucket, size=size, checksum=checksum,
url=url, meta=meta,
)
try:
col.insert_one(f.to_mongo())
except DuplicateKeyError:
# 极小概率竞争,重试
f2 = col.find_one(filt)
col.update_one(filt, upd)
return {'id': f2['_id'], 'updated': True}
return {'id': f.id, 'updated': False}
def get_files(doc_id: str) -> list[dict]:
col = get_collection(COL_FILE)
return [DocumentFile.from_mongo(d).to_dict()
for d in col.find({'doc_id': doc_id})]
def get_file_by_type(doc_id: str, file_type: str) -> dict | None:
col = get_collection(COL_FILE)
d = col.find_one({'doc_id': doc_id, 'file_type': file_type})
return DocumentFile.from_mongo(d).to_dict() if d else None