0.5 缩减重构

This commit is contained in:
lix-2026
2026-04-13 19:21:42 +08:00
parent af92c4b149
commit 71fb1aee7e
2023 changed files with 21113 additions and 394493 deletions
+1 -1
View File
@@ -1 +1 @@
"""FastAPI 应用初始化模块。"""
"""FastAPI 应用初始化模块。"""
+9 -9
View File
@@ -24,12 +24,12 @@ class Settings(BaseSettings):
openai_api_key: str = ""
lightrag_db_url: str = ""
lightrag_collection: str = "wolai-docs"
@lru_cache
def get_settings() -> Settings:
"""惰性实例化设置,避免重复读取文件。"""
return Settings()
settings = get_settings()
@lru_cache
def get_settings() -> Settings:
"""惰性实例化设置,避免重复读取文件。"""
return Settings()
settings = get_settings()
+58 -58
View File
@@ -1,58 +1,58 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
try: # Python 3.9+
from typing import Annotated # type: ignore[attr-defined]
except ImportError: # Python 3.8 fallback
from typing_extensions import Annotated
import httpx
from fastapi import Depends, Header, HTTPException, status
from app.config import settings
@dataclass
class AuthContext:
user_id: str
access_token: str
async def get_current_user(
authorization: Annotated[Optional[str], Header(convert_underscores=False)] = None,
) -> AuthContext:
"""
验证 Supabase JWTstage0 直接依赖 service_role 解析 token。
生产环境应通过 API Gateway 注入 user。
"""
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing bearer token")
token = authorization.replace("Bearer ", "", 1).strip()
if not token:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Empty token")
auth_url = f"{settings.supabase_url.rstrip('/')}/auth/v1/user"
headers = {
"Authorization": f"Bearer {token}",
"apikey": settings.supabase_service_role_key,
}
async with httpx.AsyncClient(timeout=10.0) as client:
try:
response = await client.get(auth_url, headers=headers)
response.raise_for_status()
except httpx.HTTPError as exc: # pragma: no cover - 网络异常
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Supabase token") from exc
data = response.json()
user_id = data.get("id") or data.get("user", {}).get("id")
if not user_id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Supabase token")
return AuthContext(user_id=str(user_id), access_token=token)
AuthDep = Annotated[AuthContext, Depends(get_current_user)]
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
try: # Python 3.9+
from typing import Annotated # type: ignore[attr-defined]
except ImportError: # Python 3.8 fallback
from typing_extensions import Annotated
import httpx
from fastapi import Depends, Header, HTTPException, status
from app.config import settings
@dataclass
class AuthContext:
user_id: str
access_token: str
async def get_current_user(
authorization: Annotated[Optional[str], Header(convert_underscores=False)] = None,
) -> AuthContext:
"""
验证 Supabase JWTstage0 直接依赖 service_role 解析 token。
生产环境应通过 API Gateway 注入 user。
"""
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing bearer token")
token = authorization.replace("Bearer ", "", 1).strip()
if not token:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Empty token")
auth_url = f"{settings.supabase_url.rstrip('/')}/auth/v1/user"
headers = {
"Authorization": f"Bearer {token}",
"apikey": settings.supabase_service_role_key,
}
async with httpx.AsyncClient(timeout=10.0) as client:
try:
response = await client.get(auth_url, headers=headers)
response.raise_for_status()
except httpx.HTTPError as exc: # pragma: no cover - 网络异常
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Supabase token") from exc
data = response.json()
user_id = data.get("id") or data.get("user", {}).get("id")
if not user_id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Supabase token")
return AuthContext(user_id=str(user_id), access_token=token)
AuthDep = Annotated[AuthContext, Depends(get_current_user)]
+16 -16
View File
@@ -1,12 +1,12 @@
from typing import Dict, List, Set
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.routers import api_router, root_router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.routers import api_router, root_router
app = FastAPI(title="Wolai Backend", version="0.1.0-stage0")
@@ -35,7 +35,7 @@ allow_origins = [
"http://127.0.0.1:3000",
"http://127.0.0.1:3001",
]
app.add_middleware(
CORSMiddleware,
allow_origins=allow_origins,
@@ -43,11 +43,11 @@ app.add_middleware(
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(root_router)
app.include_router(api_router)
@app.get("/")
async def root() -> Dict[str, str]:
return {"message": "Wolai backend stage0 up"}
app.include_router(root_router)
app.include_router(api_router)
@app.get("/")
async def root() -> Dict[str, str]:
return {"message": "Wolai backend stage0 up"}
+11 -11
View File
@@ -1,11 +1,11 @@
from fastapi import APIRouter
from . import chat, health, luckysheet_ws, tasks
api_router = APIRouter(prefix="/api/v1")
api_router.include_router(tasks.router, tags=["tasks"])
api_router.include_router(chat.router, tags=["chat"])
root_router = APIRouter()
root_router.include_router(health.router, tags=["health"])
root_router.include_router(luckysheet_ws.router)
from fastapi import APIRouter
from . import chat, health, luckysheet_ws, tasks
api_router = APIRouter(prefix="/api/v1")
api_router.include_router(tasks.router, tags=["tasks"])
api_router.include_router(chat.router, tags=["chat"])
root_router = APIRouter()
root_router.include_router(health.router, tags=["health"])
root_router.include_router(luckysheet_ws.router)
+26 -26
View File
@@ -1,26 +1,26 @@
import asyncio
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from app.deps import AuthDep
from app.services.lightrag_service import lightrag_service
router = APIRouter(prefix="/chat")
@router.get("")
async def chat(query: str, document_id: str, auth: AuthDep) -> StreamingResponse: # noqa: ARG001
"""
SSE 流式占位。后续会调用 LightRAG + OpenAI。
当前直接返回 mock 文字,确保前端链路可用。
"""
async def event_stream() -> asyncio.AsyncGenerator[str, None]:
reply = await lightrag_service.query(query_text=query, user_id="placeholder-user")
chunks = [reply[: len(reply) // 2 or 1], reply[len(reply) // 2 or 1 :]]
for chunk in chunks:
yield f"data: {chunk}\n\n"
await asyncio.sleep(0.1)
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream")
import asyncio
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from app.deps import AuthDep
from app.services.lightrag_service import lightrag_service
router = APIRouter(prefix="/chat")
@router.get("")
async def chat(query: str, document_id: str, auth: AuthDep) -> StreamingResponse: # noqa: ARG001
"""
SSE 流式占位。后续会调用 LightRAG + OpenAI。
当前直接返回 mock 文字,确保前端链路可用。
"""
async def event_stream() -> asyncio.AsyncGenerator[str, None]:
reply = await lightrag_service.query(query_text=query, user_id="placeholder-user")
chunks = [reply[: len(reply) // 2 or 1], reply[len(reply) // 2 or 1 :]]
for chunk in chunks:
yield f"data: {chunk}\n\n"
await asyncio.sleep(0.1)
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream")
+195 -195
View File
@@ -1,195 +1,195 @@
from __future__ import annotations
import asyncio
import json
import logging
import urllib.parse
import zlib
from dataclasses import dataclass
from typing import Dict, List, Optional
import httpx
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, status
from starlette.concurrency import run_in_threadpool
from app.config import settings
from app.services.supabase_rest import supabase_rest
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/ws", tags=["luckysheet"])
@dataclass
class LuckysheetClient:
websocket: WebSocket
user_id: str
username: str
class LuckysheetConnectionManager:
def __init__(self) -> None:
self._clients: Dict[str, List[LuckysheetClient]] = {}
self._lock = asyncio.Lock()
async def add(self, grid_key: str, client: LuckysheetClient) -> None:
async with self._lock:
self._clients.setdefault(grid_key, []).append(client)
logger.debug("Luckysheet client %s joined grid %s", client.user_id, grid_key)
async def remove(self, grid_key: str, websocket: WebSocket) -> None:
async with self._lock:
clients = self._clients.get(grid_key)
if not clients:
return
self._clients[grid_key] = [client for client in clients if client.websocket is not websocket]
if not self._clients[grid_key]:
self._clients.pop(grid_key, None)
logger.debug("Luckysheet connection removed from grid %s", grid_key)
async def broadcast_payload(
self,
grid_key: str,
sender: LuckysheetClient,
payload: str,
event_type: int,
) -> None:
clients = self._clients.get(grid_key, [])
if not clients:
return
message = json.dumps(
{
"data": payload,
"id": sender.user_id,
"username": sender.username,
"type": event_type,
},
ensure_ascii=False,
)
for client in clients:
if client.websocket is sender.websocket:
continue
try:
await client.websocket.send_text(message)
except Exception as exc:
logger.warning("Failed to forward message to %s: %s", client.user_id, exc)
async def broadcast_exit(self, grid_key: str, user_id: str) -> None:
clients = self._clients.get(grid_key, [])
if not clients:
return
message = json.dumps({"message": "用户退出", "id": user_id}, ensure_ascii=False)
for client in clients:
try:
await client.websocket.send_text(message)
except Exception as exc:
logger.warning("Failed to notify client exit: %s", exc)
manager = LuckysheetConnectionManager()
def _decode_ws_payload(raw_message: str) -> Optional[dict]:
try:
compressed = raw_message.encode("latin1")
inflated = zlib.decompress(compressed)
decoded = urllib.parse.unquote(inflated.decode("utf-8"))
return json.loads(decoded)
except Exception as exc:
logger.debug("Failed to decode luckysheet payload: %s", exc)
return None
async def _fetch_supabase_user(access_token: str) -> Optional[dict]:
base_url = settings.supabase_url.rstrip("/")
headers = {
"apikey": settings.supabase_service_role_key,
"Authorization": f"Bearer {access_token}",
}
try:
async with httpx.AsyncClient(timeout=5.0) as client:
response = await client.get(f"{base_url}/auth/v1/user", headers=headers)
response.raise_for_status()
return response.json()
except httpx.HTTPError as exc:
logger.warning("Supabase auth verification failed: %s", exc)
return None
async def _fetch_table_by_grid_key(grid_key: str) -> Optional[dict]:
try:
return await run_in_threadpool(lambda: supabase_rest.select_one("document_tables", {"grid_key": grid_key}))
except Exception as exc:
logger.error("Failed to query document_tables: %s", exc)
return None
async def _is_workspace_member(workspace_id: str, user_id: str) -> bool:
try:
result = await run_in_threadpool(
lambda: supabase_rest.select_one("workspace_members", {"workspace_id": workspace_id, "user_id": user_id}),
)
return bool(result)
except Exception as exc:
logger.error("Failed to verify workspace membership: %s", exc)
return False
@router.websocket("/luckysheet")
async def luckysheet_collaboration(websocket: WebSocket) -> None:
params = websocket.query_params
grid_key = params.get("gridKey")
token = params.get("token")
raw_user_id = params.get("userid") or params.get("userId")
requested_type = params.get("type") or "luckysheet"
username = params.get("username") or ""
if requested_type != "luckysheet" or not grid_key or not token or not raw_user_id:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="缺少协同参数")
return
supabase_user = await _fetch_supabase_user(token)
if not supabase_user or supabase_user.get("id") != raw_user_id:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="身份验证失败")
return
table_row = await _fetch_table_by_grid_key(grid_key)
if not table_row or not table_row.get("workspace_id"):
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="gridKey 无效")
return
workspace_id = table_row["workspace_id"]
if not await _is_workspace_member(workspace_id, raw_user_id):
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="无权访问该表格")
return
fallback_name = (
username
or supabase_user.get("user_metadata", {}).get("full_name")
or supabase_user.get("email")
or f"用户-{raw_user_id[:6]}"
)
client = LuckysheetClient(websocket=websocket, user_id=raw_user_id, username=fallback_name)
await websocket.accept()
await manager.add(grid_key, client)
logger.info("Luckysheet client %s connected to %s", raw_user_id, grid_key)
try:
while True:
message = await websocket.receive_text()
if message == "rub":
continue
decoded = _decode_ws_payload(message)
if not decoded:
continue
op_type = decoded.get("t")
event_type = 3 if op_type == "mv" else 2
await manager.broadcast_payload(grid_key, client, message, event_type)
except WebSocketDisconnect:
logger.info("Luckysheet client %s disconnected", raw_user_id)
except Exception as exc:
logger.error("Luckysheet websocket error: %s", exc)
finally:
await manager.remove(grid_key, websocket)
await manager.broadcast_exit(grid_key, raw_user_id)
from __future__ import annotations
import asyncio
import json
import logging
import urllib.parse
import zlib
from dataclasses import dataclass
from typing import Dict, List, Optional
import httpx
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, status
from starlette.concurrency import run_in_threadpool
from app.config import settings
from app.services.supabase_rest import supabase_rest
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/ws", tags=["luckysheet"])
@dataclass
class LuckysheetClient:
websocket: WebSocket
user_id: str
username: str
class LuckysheetConnectionManager:
def __init__(self) -> None:
self._clients: Dict[str, List[LuckysheetClient]] = {}
self._lock = asyncio.Lock()
async def add(self, grid_key: str, client: LuckysheetClient) -> None:
async with self._lock:
self._clients.setdefault(grid_key, []).append(client)
logger.debug("Luckysheet client %s joined grid %s", client.user_id, grid_key)
async def remove(self, grid_key: str, websocket: WebSocket) -> None:
async with self._lock:
clients = self._clients.get(grid_key)
if not clients:
return
self._clients[grid_key] = [client for client in clients if client.websocket is not websocket]
if not self._clients[grid_key]:
self._clients.pop(grid_key, None)
logger.debug("Luckysheet connection removed from grid %s", grid_key)
async def broadcast_payload(
self,
grid_key: str,
sender: LuckysheetClient,
payload: str,
event_type: int,
) -> None:
clients = self._clients.get(grid_key, [])
if not clients:
return
message = json.dumps(
{
"data": payload,
"id": sender.user_id,
"username": sender.username,
"type": event_type,
},
ensure_ascii=False,
)
for client in clients:
if client.websocket is sender.websocket:
continue
try:
await client.websocket.send_text(message)
except Exception as exc:
logger.warning("Failed to forward message to %s: %s", client.user_id, exc)
async def broadcast_exit(self, grid_key: str, user_id: str) -> None:
clients = self._clients.get(grid_key, [])
if not clients:
return
message = json.dumps({"message": "用户退出", "id": user_id}, ensure_ascii=False)
for client in clients:
try:
await client.websocket.send_text(message)
except Exception as exc:
logger.warning("Failed to notify client exit: %s", exc)
manager = LuckysheetConnectionManager()
def _decode_ws_payload(raw_message: str) -> Optional[dict]:
try:
compressed = raw_message.encode("latin1")
inflated = zlib.decompress(compressed)
decoded = urllib.parse.unquote(inflated.decode("utf-8"))
return json.loads(decoded)
except Exception as exc:
logger.debug("Failed to decode luckysheet payload: %s", exc)
return None
async def _fetch_supabase_user(access_token: str) -> Optional[dict]:
base_url = settings.supabase_url.rstrip("/")
headers = {
"apikey": settings.supabase_service_role_key,
"Authorization": f"Bearer {access_token}",
}
try:
async with httpx.AsyncClient(timeout=5.0) as client:
response = await client.get(f"{base_url}/auth/v1/user", headers=headers)
response.raise_for_status()
return response.json()
except httpx.HTTPError as exc:
logger.warning("Supabase auth verification failed: %s", exc)
return None
async def _fetch_table_by_grid_key(grid_key: str) -> Optional[dict]:
try:
return await run_in_threadpool(lambda: supabase_rest.select_one("document_tables", {"grid_key": grid_key}))
except Exception as exc:
logger.error("Failed to query document_tables: %s", exc)
return None
async def _is_workspace_member(workspace_id: str, user_id: str) -> bool:
try:
result = await run_in_threadpool(
lambda: supabase_rest.select_one("workspace_members", {"workspace_id": workspace_id, "user_id": user_id}),
)
return bool(result)
except Exception as exc:
logger.error("Failed to verify workspace membership: %s", exc)
return False
@router.websocket("/luckysheet")
async def luckysheet_collaboration(websocket: WebSocket) -> None:
params = websocket.query_params
grid_key = params.get("gridKey")
token = params.get("token")
raw_user_id = params.get("userid") or params.get("userId")
requested_type = params.get("type") or "luckysheet"
username = params.get("username") or ""
if requested_type != "luckysheet" or not grid_key or not token or not raw_user_id:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="缺少协同参数")
return
supabase_user = await _fetch_supabase_user(token)
if not supabase_user or supabase_user.get("id") != raw_user_id:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="身份验证失败")
return
table_row = await _fetch_table_by_grid_key(grid_key)
if not table_row or not table_row.get("workspace_id"):
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="gridKey 无效")
return
workspace_id = table_row["workspace_id"]
if not await _is_workspace_member(workspace_id, raw_user_id):
await websocket.close(code=status.WS_1008_POLICY_VIOLATION, reason="无权访问该表格")
return
fallback_name = (
username
or supabase_user.get("user_metadata", {}).get("full_name")
or supabase_user.get("email")
or f"用户-{raw_user_id[:6]}"
)
client = LuckysheetClient(websocket=websocket, user_id=raw_user_id, username=fallback_name)
await websocket.accept()
await manager.add(grid_key, client)
logger.info("Luckysheet client %s connected to %s", raw_user_id, grid_key)
try:
while True:
message = await websocket.receive_text()
if message == "rub":
continue
decoded = _decode_ws_payload(message)
if not decoded:
continue
op_type = decoded.get("t")
event_type = 3 if op_type == "mv" else 2
await manager.broadcast_payload(grid_key, client, message, event_type)
except WebSocketDisconnect:
logger.info("Luckysheet client %s disconnected", raw_user_id)
except Exception as exc:
logger.error("Luckysheet websocket error: %s", exc)
finally:
await manager.remove(grid_key, websocket)
await manager.broadcast_exit(grid_key, raw_user_id)
+29 -29
View File
@@ -1,29 +1,29 @@
from fastapi import APIRouter, HTTPException, status
from app.deps import AuthDep
from app.schemas.tasks import OcrTaskRequest, TaskStatusResponse
from app.services.task_tracker import task_tracker
from app.workers.tasks import ocr_pipeline
router = APIRouter(prefix="/tasks")
@router.post("/ocr", response_model=TaskStatusResponse)
async def enqueue_ocr_task(payload: OcrTaskRequest, auth: AuthDep) -> TaskStatusResponse:
"""记录任务并投递 Celery,阶段 0 返回占位任务。"""
task = task_tracker.create_task(user_id=auth.user_id, document_id=payload.document_id, task_type="ocr")
ocr_pipeline.delay(
task_id=task.task_id,
document_id=payload.document_id,
file_url=str(payload.file_url),
user_id=auth.user_id,
)
return task
@router.get("/{task_id}", response_model=TaskStatusResponse)
async def get_task_status(task_id: str, auth: AuthDep) -> TaskStatusResponse:
task = task_tracker.get_task(task_id=task_id, user_id=auth.user_id)
if not task:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Task not found")
return task
from fastapi import APIRouter, HTTPException, status
from app.deps import AuthDep
from app.schemas.tasks import OcrTaskRequest, TaskStatusResponse
from app.services.task_tracker import task_tracker
from app.workers.tasks import ocr_pipeline
router = APIRouter(prefix="/tasks")
@router.post("/ocr", response_model=TaskStatusResponse)
async def enqueue_ocr_task(payload: OcrTaskRequest, auth: AuthDep) -> TaskStatusResponse:
"""记录任务并投递 Celery,阶段 0 返回占位任务。"""
task = task_tracker.create_task(user_id=auth.user_id, document_id=payload.document_id, task_type="ocr")
ocr_pipeline.delay(
task_id=task.task_id,
document_id=payload.document_id,
file_url=str(payload.file_url),
user_id=auth.user_id,
)
return task
@router.get("/{task_id}", response_model=TaskStatusResponse)
async def get_task_status(task_id: str, auth: AuthDep) -> TaskStatusResponse:
task = task_tracker.get_task(task_id=task_id, user_id=auth.user_id)
if not task:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Task not found")
return task
+17 -17
View File
@@ -1,17 +1,17 @@
from __future__ import annotations
from typing import Literal, Optional
from pydantic import AnyHttpUrl, BaseModel
class OcrTaskRequest(BaseModel):
document_id: str
file_url: AnyHttpUrl
class TaskStatusResponse(BaseModel):
task_id: str
status: Literal["pending", "processing", "completed", "failed"]
progress: int = 0
message: Optional[str] = None
from __future__ import annotations
from typing import Literal, Optional
from pydantic import AnyHttpUrl, BaseModel
class OcrTaskRequest(BaseModel):
document_id: str
file_url: AnyHttpUrl
class TaskStatusResponse(BaseModel):
task_id: str
status: Literal["pending", "processing", "completed", "failed"]
progress: int = 0
message: Optional[str] = None
+19 -19
View File
@@ -1,19 +1,19 @@
"""LightRAG 集成占位。阶段 1 会在这里封装真正的查询与索引。"""
from app.config import settings
class LightRAGService:
def __init__(self) -> None:
self.collection = settings.lightrag_collection
async def queue_index(self, document_id: str, raw_text: str) -> None:
"""预留方法:后续调用 LightRAG.update_index。"""
return None
async def query(self, query_text: str, user_id: str) -> str:
"""预留方法:后续调用 LightRAG.query,当前返回占位回答。"""
return f"[mock] {query_text}"
lightrag_service = LightRAGService()
"""LightRAG 集成占位。阶段 1 会在这里封装真正的查询与索引。"""
from app.config import settings
class LightRAGService:
def __init__(self) -> None:
self.collection = settings.lightrag_collection
async def queue_index(self, document_id: str, raw_text: str) -> None:
"""预留方法:后续调用 LightRAG.update_index。"""
return None
async def query(self, query_text: str, user_id: str) -> str:
"""预留方法:后续调用 LightRAG.query,当前返回占位回答。"""
return f"[mock] {query_text}"
lightrag_service = LightRAGService()
+13 -13
View File
@@ -1,13 +1,13 @@
"""MinerU OCR 占位服务。"""
class MinerUService:
async def extract_markdown(self, file_url: str) -> str:
"""
阶段 0:直接返回固定内容,保证前端流程贯通。
阶段 1:调用 MinerU CLI / SDK,从 Supabase Storage 下载文件后解析。
"""
return f"# OCR Placeholder\n\n源文件:{file_url}"
mineru_service = MinerUService()
"""MinerU OCR 占位服务。"""
class MinerUService:
async def extract_markdown(self, file_url: str) -> str:
"""
阶段 0:直接返回固定内容,保证前端流程贯通。
阶段 1:调用 MinerU CLI / SDK,从 Supabase Storage 下载文件后解析。
"""
return f"# OCR Placeholder\n\n源文件:{file_url}"
mineru_service = MinerUService()
+57 -57
View File
@@ -1,57 +1,57 @@
from __future__ import annotations
from typing import Any, Dict, Optional
import httpx
from app.config import settings
class SupabaseRestClient:
"""轻量封装 Supabase RESTful API,兼容本地 sb_secret 密钥。"""
def __init__(self) -> None:
base_url = settings.supabase_url.rstrip("/")
self.client = httpx.Client(
base_url=f"{base_url}/rest/v1",
headers={
"apikey": settings.supabase_service_role_key,
"Authorization": f"Bearer {settings.supabase_service_role_key}",
},
timeout=10.0,
)
def insert(self, table: str, payload: Dict[str, Any]) -> Dict[str, Any]:
response = self.client.post(
f"/{table}",
json=payload,
headers={"Prefer": "return=representation"},
)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return data[0]
return data
def select_one(self, table: str, filters: Dict[str, Any]) -> Optional[Dict[str, Any]]:
params = {key: f"eq.{value}" for key, value in filters.items()}
params["select"] = "*"
response = self.client.get(f"/{table}", params=params)
response.raise_for_status()
data = response.json()
if isinstance(data, list) and data:
return data[0]
return None
def update(self, table: str, filters: Dict[str, Any], payload: Dict[str, Any]) -> None:
params = {key: f"eq.{value}" for key, value in filters.items()}
response = self.client.patch(
f"/{table}",
params=params,
json=payload,
headers={"Prefer": "return=minimal"},
)
response.raise_for_status()
supabase_rest = SupabaseRestClient()
from __future__ import annotations
from typing import Any, Dict, Optional
import httpx
from app.config import settings
class SupabaseRestClient:
"""轻量封装 Supabase RESTful API,兼容本地 sb_secret 密钥。"""
def __init__(self) -> None:
base_url = settings.supabase_url.rstrip("/")
self.client = httpx.Client(
base_url=f"{base_url}/rest/v1",
headers={
"apikey": settings.supabase_service_role_key,
"Authorization": f"Bearer {settings.supabase_service_role_key}",
},
timeout=10.0,
)
def insert(self, table: str, payload: Dict[str, Any]) -> Dict[str, Any]:
response = self.client.post(
f"/{table}",
json=payload,
headers={"Prefer": "return=representation"},
)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return data[0]
return data
def select_one(self, table: str, filters: Dict[str, Any]) -> Optional[Dict[str, Any]]:
params = {key: f"eq.{value}" for key, value in filters.items()}
params["select"] = "*"
response = self.client.get(f"/{table}", params=params)
response.raise_for_status()
data = response.json()
if isinstance(data, list) and data:
return data[0]
return None
def update(self, table: str, filters: Dict[str, Any], payload: Dict[str, Any]) -> None:
params = {key: f"eq.{value}" for key, value in filters.items()}
response = self.client.patch(
f"/{table}",
params=params,
json=payload,
headers={"Prefer": "return=minimal"},
)
response.raise_for_status()
supabase_rest = SupabaseRestClient()
+15 -15
View File
@@ -1,15 +1,15 @@
from celery import Celery
from app.config import settings
def create_celery_app() -> Celery:
app = Celery("wolai-backend")
app.conf.broker_url = settings.redis_url
app.conf.result_backend = settings.redis_url
app.conf.task_routes = {"app.workers.tasks.*": {"queue": "wolai-tasks"}}
app.autodiscover_tasks(["app.workers"])
return app
celery_app = create_celery_app()
from celery import Celery
from app.config import settings
def create_celery_app() -> Celery:
app = Celery("wolai-backend")
app.conf.broker_url = settings.redis_url
app.conf.result_backend = settings.redis_url
app.conf.task_routes = {"app.workers.tasks.*": {"queue": "wolai-tasks"}}
app.autodiscover_tasks(["app.workers"])
return app
celery_app = create_celery_app()
+51 -51
View File
@@ -1,51 +1,51 @@
from __future__ import annotations
from typing import Dict
from celery import shared_task
from app.services.supabase_rest import supabase_rest
@shared_task(name="app.workers.tasks.ocr_pipeline")
def ocr_pipeline(task_id: str, document_id: str, file_url: str, user_id: str) -> Dict[str, str]:
"""
阶段 0 Celery 任务:模拟 OCR,向 documents+background_tasks 回写占位结果。
阶段 1 将在此处串联 MinerU OCR 与 LightRAG 索引。
"""
supabase_rest.update("background_tasks", {"id": task_id}, {"status": "processing", "progress": 30})
markdown = f"# OCR 结果占位\\n\\n文件地址:{file_url}\\n\\n> 阶段 1 将替换为 MinerU 输出。"
supabase_rest.update(
"documents",
{"id": document_id, "user_id": user_id},
{
"content": {
"blocks": [
{
"type": "paragraph",
"text": markdown,
}
]
},
"raw_text": markdown,
"index_status": "completed",
},
)
supabase_rest.update(
"background_tasks",
{"id": task_id},
{
"status": "completed",
"progress": 100,
"message": "OCR 模拟完成",
},
)
return {
"task_id": task_id,
"document_id": document_id,
"file_url": file_url,
"status": "completed",
}
from __future__ import annotations
from typing import Dict
from celery import shared_task
from app.services.supabase_rest import supabase_rest
@shared_task(name="app.workers.tasks.ocr_pipeline")
def ocr_pipeline(task_id: str, document_id: str, file_url: str, user_id: str) -> Dict[str, str]:
"""
阶段 0 Celery 任务:模拟 OCR,向 documents+background_tasks 回写占位结果。
阶段 1 将在此处串联 MinerU OCR 与 LightRAG 索引。
"""
supabase_rest.update("background_tasks", {"id": task_id}, {"status": "processing", "progress": 30})
markdown = f"# OCR 结果占位\\n\\n文件地址:{file_url}\\n\\n> 阶段 1 将替换为 MinerU 输出。"
supabase_rest.update(
"documents",
{"id": document_id, "user_id": user_id},
{
"content": {
"blocks": [
{
"type": "paragraph",
"text": markdown,
}
]
},
"raw_text": markdown,
"index_status": "completed",
},
)
supabase_rest.update(
"background_tasks",
{"id": task_id},
{
"status": "completed",
"progress": 100,
"message": "OCR 模拟完成",
},
)
return {
"task_id": task_id,
"document_id": document_id,
"file_url": file_url,
"status": "completed",
}
+3025 -3025
View File
File diff suppressed because it is too large Load Diff
+19 -19
View File
@@ -1,19 +1,19 @@
-------------- celery@LIX v5.4.0 (opalescent)
--- ***** -----
-- ******* ---- Windows-10-10.0.26100-SP0 2025-11-18 18:34:36
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: wolai-backend:0x2b6d01c47d0
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * --- .> concurrency: 24 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. app.workers.tasks.ocr_pipeline
-------------- celery@LIX v5.4.0 (opalescent)
--- ***** -----
-- ******* ---- Windows-10-10.0.26100-SP0 2025-11-18 18:34:36
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: wolai-backend:0x2b6d01c47d0
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * --- .> concurrency: 24 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. app.workers.tasks.ocr_pipeline
+11 -11
View File
@@ -1,11 +1,11 @@
fastapi==0.115.5
uvicorn[standard]==0.33.0
celery[redis]==5.4.0
redis==5.1.1
httpx==0.27.2
python-dotenv==1.0.1
pgvector==0.3.5
openai==1.59.5
python-multipart==0.0.17
pydantic-settings==2.6.1
typing_extensions==4.12.2
fastapi==0.115.5
uvicorn[standard]==0.33.0
celery[redis]==5.4.0
redis==5.1.1
httpx==0.27.2
python-dotenv==1.0.1
pgvector==0.3.5
openai==1.59.5
python-multipart==0.0.17
pydantic-settings==2.6.1
typing_extensions==4.12.2
+4 -4
View File
@@ -1,4 +1,4 @@
INFO: Started server process [27148]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started server process [27148]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
+4 -4
View File
@@ -1,4 +1,4 @@
INFO: Started server process [10528]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started server process [10528]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
+30 -30
View File
@@ -1,30 +1,30 @@
INFO: 127.0.0.1:56337 - "GET / HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 127.0.0.1:56337 - "GET / HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 183.221.18.13:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
INFO: 2409:8a62:e45:7c51::16f3:0 - "GET /health HTTP/1.1" 200 OK
+57 -57
View File
@@ -1,57 +1,57 @@
INFO: Will watch for changes in these directories: ['F:\\SOFT\\MNOTE\\wolai-backend']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [20384] using WatchFiles
INFO: Started server process [6920]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING: WatchFiles detected changes in 'app\main.py'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [6920]
INFO: Started server process [5608]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING: WatchFiles detected changes in 'app\deps.py'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [5608]
WARNING: WatchFiles detected changes in 'app\deps.py', 'app\routers\tasks.py', 'app\workers\tasks.py', 'app\services\task_tracker.py'. Reloading...
INFO: Started server process [33148]
INFO: Waiting for application startup.
INFO: Application startup complete.
ERROR: Traceback (most recent call last):
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 123, in run
raise KeyboardInterrupt()
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\SOFT\MNOTE\wolai-backend\venv\Lib\site-packages\starlette\routing.py", line 700, in lifespan
await receive()
File "F:\SOFT\MNOTE\wolai-backend\venv\Lib\site-packages\uvicorn\lifespan\on.py", line 137, in receive
return await self.receive_queue.get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\queues.py", line 158, in get
await getter
asyncio.exceptions.CancelledError
INFO: Started server process [12156]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Will watch for changes in these directories: ['F:\\SOFT\\MNOTE\\wolai-backend']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [20384] using WatchFiles
INFO: Started server process [6920]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING: WatchFiles detected changes in 'app\main.py'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [6920]
INFO: Started server process [5608]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING: WatchFiles detected changes in 'app\deps.py'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [5608]
WARNING: WatchFiles detected changes in 'app\deps.py', 'app\routers\tasks.py', 'app\workers\tasks.py', 'app\services\task_tracker.py'. Reloading...
INFO: Started server process [33148]
INFO: Waiting for application startup.
INFO: Application startup complete.
ERROR: Traceback (most recent call last):
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 123, in run
raise KeyboardInterrupt()
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\SOFT\MNOTE\wolai-backend\venv\Lib\site-packages\starlette\routing.py", line 700, in lifespan
await receive()
File "F:\SOFT\MNOTE\wolai-backend\venv\Lib\site-packages\uvicorn\lifespan\on.py", line 137, in receive
return await self.receive_queue.get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\liaib\AppData\Local\Programs\Python\Python311\Lib\asyncio\queues.py", line 158, in get
await getter
asyncio.exceptions.CancelledError
INFO: Started server process [12156]
INFO: Waiting for application startup.
INFO: Application startup complete.
+1 -1
View File
@@ -1 +1 @@
INFO: 127.0.0.1:57108 - "GET /health HTTP/1.1" 200 OK
INFO: 127.0.0.1:57108 - "GET /health HTTP/1.1" 200 OK