hit-cxf
2026-05-31 0bc7753fba9b44d4caa5d12bc4075fd402aa689a
video-claw/video-claw/backend/api/routers/sandbox.py
@@ -1,6 +1,7 @@
import json
import logging
import os
import threading
import uuid
from datetime import datetime
from typing import List
@@ -24,6 +25,7 @@
SANDBOX_DIR = os.path.join(settings.CODE_DIR, "result", "sandbox")
SANDBOX_HISTORY_FILE = os.path.join(SANDBOX_DIR, "history.json")
SANDBOX_ACTIVE_TASKS: dict[str, dict] = {}
SANDBOX_LOCK = threading.RLock()
# 确保目录存在
os.makedirs(SANDBOX_DIR, exist_ok=True)
@@ -31,6 +33,7 @@
def _load_history() -> List[dict]:
    """加载历史记录"""
    with SANDBOX_LOCK:
    if os.path.exists(SANDBOX_HISTORY_FILE):
        try:
            with open(SANDBOX_HISTORY_FILE, 'r', encoding='utf-8') as f:
@@ -43,8 +46,11 @@
def _save_history(history: List[dict]):
    """保存历史记录"""
    with open(SANDBOX_HISTORY_FILE, 'w', encoding='utf-8') as f:
    with SANDBOX_LOCK:
        tmp_path = f"{SANDBOX_HISTORY_FILE}.{uuid.uuid4().hex}.tmp"
        with open(tmp_path, 'w', encoding='utf-8') as f:
        json.dump(history, f, ensure_ascii=False, indent=2)
        os.replace(tmp_path, SANDBOX_HISTORY_FILE)
def _normalize_path(path: str) -> str:
@@ -88,6 +94,7 @@
def _add_record(tool: str, model: str, input_data: dict, output_data: dict, files: List[str] = None, record_id: str | None = None) -> str:
    """添加历史记录"""
    with SANDBOX_LOCK:
    record_id = record_id or str(uuid.uuid4().hex[:8])
    # 转换路径为相对路径格式
    output_data = _convert_output_paths(output_data)
@@ -107,6 +114,7 @@
def _start_active_task(tool: str, model: str, input_data: dict) -> str:
    with SANDBOX_LOCK:
    task_id = str(uuid.uuid4().hex[:8])
    SANDBOX_ACTIVE_TASKS[task_id] = {
        "id": task_id,
@@ -121,6 +129,7 @@
def _finish_active_task(task_id: str) -> None:
    with SANDBOX_LOCK:
    SANDBOX_ACTIVE_TASKS.pop(task_id, None)
@@ -160,7 +169,9 @@
@router.get("/api/sandbox/tasks")
async def sandbox_get_active_tasks():
    """获取临时工作台正在执行的任务"""
    return {"success": True, "tasks": list(SANDBOX_ACTIVE_TASKS.values())}
    with SANDBOX_LOCK:
        tasks = list(SANDBOX_ACTIVE_TASKS.values())
    return {"success": True, "tasks": tasks}
@router.get("/api/sandbox/history/{record_id}")
@@ -176,6 +187,7 @@
@router.delete("/api/sandbox/history/{record_id}")
async def sandbox_delete_record(record_id: str):
    """删除历史记录"""
    with SANDBOX_LOCK:
    history = _load_history()
    record_to_delete = None
    new_history = []
@@ -188,9 +200,10 @@
    if record_to_delete is None:
        return {"success": False, "error": "记录不存在"}
    # 删除关联的文件
    _delete_record_files(record_to_delete.get("files", []))
    _save_history(new_history)
    # 删除关联文件不需要占用历史锁。
    _delete_record_files(record_to_delete.get("files", []))
    logger.info("Sandbox history deleted: record_id=%s", record_id)
    return {"success": True}