refactor: 收口 V3 分层架构与插件兼容边界

This commit is contained in:
jxxghp
2026-08-18 13:22:02 +08:00
parent cca99bd421
commit 8472bcff43
274 changed files with 10730 additions and 6130 deletions
+22 -73
View File
@@ -3,8 +3,6 @@ import time
from typing import List, Any, Optional
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
from app.schemas.common import BatchProgressKeyData as _SchemaBatchProgressKeyData
from app.schemas.common import ProgressKeyData as _SchemaProgressKeyData
@@ -22,23 +20,20 @@ from app.agent.prompt.transfer_redo import (
build_manual_redo_prompt,
)
from app.runtime.config import settings, global_vars
from app.application.security.access import verify_token
from app.db import get_async_db, get_db
from app.db.models import User
from app.db.models.downloadhistory import DownloadHistory
from app.db.models.transferhistory import TransferHistory
from app.adapters.web.security.access import verify_token
from app.api.deps import (
get_current_active_manage_user,
get_current_active_superuser,
get_download_history_mutation_command,
get_history_query_service,
get_transfer_history_mutation_command,
)
from app.runtime.progress import ProgressHelper
from app.application.history import (
DownloadHistoryMutationCommand,
HistoryQueryService,
TransferHistoryMutationCommand,
)
from app.foundation.text import cut as jieba_cut
from app.runtime.log import logger
router = ResponseAPIRouter()
@@ -155,13 +150,13 @@ def _start_batch_ai_redo_task(
async def download_history(
page: Optional[int] = 1,
count: Optional[int] = 30,
db: AsyncSession = Depends(get_async_db),
query: HistoryQueryService = Depends(get_history_query_service),
_: _SchemaTokenPayload = Depends(verify_token),
) -> Any:
"""
按下载时间倒序查询下载历史记录
"""
return await DownloadHistory.async_list_by_page(db, page, count)
return await query.list_download(page=page, count=count)
@router.delete(
@@ -183,14 +178,6 @@ def delete_download_history(
return _SchemaResponse(success=result.success, message=result.message)
def _glob_to_like(pattern: str) -> str:
"""
将 glob 通配符模式转换为 SQL LIKE 模式(使用 \\ 作为转义字符)
"""
result = pattern.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
return result.replace("*", "%").replace("?", "_")
@router.get(
"/transfer",
summary="查询整理记录",
@@ -201,50 +188,19 @@ async def transfer_history(
page: Optional[int] = 1,
count: Optional[int] = 30,
status: Optional[bool] = None,
db: AsyncSession = Depends(get_async_db),
query: HistoryQueryService = Depends(get_history_query_service),
_: _SchemaTokenPayload = Depends(verify_token),
) -> Any:
"""
查询整理记录,title 支持通配符 * 和 ?(如 *.mkv、*2024*
"""
if title == "失败":
title = None
status = False
elif title == "成功":
title = None
status = True
if title:
if "*" in title or "?" in title:
like_pattern = _glob_to_like(title)
total = await TransferHistory.async_count_by_title(
db, title=like_pattern, status=status, wildcard=True
)
result = await TransferHistory.async_list_by_title(
db, title=like_pattern, page=page, count=count, status=status, wildcard=True
)
else:
words = jieba_cut(title, HMM=False)
like_pattern = "%".join(words)
total = await TransferHistory.async_count_by_title(
db, title=like_pattern, status=status
)
result = await TransferHistory.async_list_by_title(
db, title=like_pattern, page=page, count=count, status=status
)
else:
result = await TransferHistory.async_list_by_page(
db, page=page, count=count, status=status
)
total = await TransferHistory.async_count(db, status=status)
return _SchemaResponse(
success=True,
data={
"list": [item.to_dict() for item in result],
"total": total,
},
result = await query.list_transfer(
title=title,
page=page,
count=count,
status=status,
)
return _SchemaResponse(success=True, data=result)
@router.delete("/transfer", summary="删除整理记录", response_model=_SchemaResponse[None])
@@ -255,7 +211,7 @@ def delete_transfer_history(
command: TransferHistoryMutationCommand = Depends(
get_transfer_history_mutation_command
),
_: User = Depends(get_current_active_manage_user),
_: object = Depends(get_current_active_manage_user),
) -> Any:
"""
删除整理记录。
@@ -273,10 +229,10 @@ def delete_transfer_history(
summary="智能助手重新整理",
response_model=_SchemaResponse[_SchemaProgressKeyData],
)
def ai_redo_transfer_history(
async def ai_redo_transfer_history(
history_id: int,
db: Session = Depends(get_db),
_: User = Depends(get_current_active_manage_user),
query: HistoryQueryService = Depends(get_history_query_service),
_: object = Depends(get_current_active_manage_user),
) -> Any:
"""
手动触发单条历史记录的 AI 重新整理,并返回进度键。
@@ -284,7 +240,7 @@ def ai_redo_transfer_history(
if not settings.AI_AGENT_ENABLE:
return _SchemaResponse(success=False, message="MoviePilot智能助手未启用")
history = TransferHistory.get(db, history_id)
history = await query.get_transfer(history_id)
if not history:
return _SchemaResponse(success=False, message="整理记录不存在")
@@ -304,10 +260,10 @@ def ai_redo_transfer_history(
summary="智能助手批量重新整理",
response_model=_SchemaResponse[_SchemaBatchProgressKeyData],
)
def batch_ai_redo_transfer_history(
async def batch_ai_redo_transfer_history(
payload: _SchemaBatchTransferHistoryRedoRequest,
db: Session = Depends(get_db),
_: User = Depends(get_current_active_manage_user),
query: HistoryQueryService = Depends(get_history_query_service),
_: object = Depends(get_current_active_manage_user),
) -> Any:
"""
手动触发多条历史记录的 AI 批量重新整理,并返回进度键。
@@ -319,14 +275,7 @@ def batch_ai_redo_transfer_history(
if not history_ids:
return _SchemaResponse(success=False, message="未提供有效的整理记录")
histories = []
missing_ids = []
for history_id in history_ids:
history = TransferHistory.get(db, history_id)
if not history:
missing_ids.append(history_id)
continue
histories.append(history)
histories, missing_ids = await query.get_transfers(history_ids)
if missing_ids:
return _SchemaResponse(
@@ -358,7 +307,7 @@ def empty_transfer_history(
command: TransferHistoryMutationCommand = Depends(
get_transfer_history_mutation_command
),
_: User = Depends(get_current_active_superuser),
_: object = Depends(get_current_active_superuser),
) -> Any:
"""
清空整理记录