diff --git a/prompt/system_prompt.md b/prompt/system_prompt.md index dc9edd9..5bbb5a8 100644 --- a/prompt/system_prompt.md +++ b/prompt/system_prompt.md @@ -56,15 +56,17 @@ When creating scripts in `executable_code/`, follow these organization rules: - Example: `executable_code/invoice_parser/parse_invoice.py` for invoice parsing scripts - Example: `executable_code/data_extractor/extract.py` for data extraction scripts -- **Temporary Scripts**: Place short-lived or experimental scripts in tmp subfolder - - Format: `executable_code/tmp/script.py` - - Example: `executable_code/tmp/debug_test.py` for debugging scripts +- **Temporary Scripts**: AVOID creating temporary script files when possible + - **Preferred**: Use `python -c "..."` for one-off scripts (inline execution) + - **Fallback**: Only create files if the script is too complex or requires file persistence + - **Location**: `executable_code/tmp/script.py` (when file creation is necessary) + - **Cleanup**: Files in `{agent_dir_path}/executable_code/tmp/` older than 3 days will be automatically deleted **Path Examples:** - Skill script: `{agent_dir_path}/skills/rag-retrieve/scripts/rag_retrieve.py` - Dataset file: `{agent_dir_path}/dataset/document.txt` - Task-specific script: `{agent_dir_path}/executable_code/invoice_parser/parse.py` -- Temporary script: `{agent_dir_path}/executable_code/tmp/test.py` +- Temporary script (when needed): `{agent_dir_path}/executable_code/tmp/test.py` - Downloaded file: `{agent_dir_path}/download/report.pdf` ## System Information diff --git a/routes/chat.py b/routes/chat.py index 1fb6e0e..597b748 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -1,6 +1,8 @@ import json import os import asyncio +import shutil +import time from typing import Union, Optional, Any, List, Dict from fastapi import APIRouter, HTTPException, Header from fastapi.responses import StreamingResponse @@ -393,6 +395,41 @@ async def _execute_post_agent_hooks(config: AgentConfig, response: str) -> None: # hook执行失败不影响主流程 logger.error(f"Failed to execute PostAgent hooks: {e}") + # 清理 executable_code/tmp 文件夹 + await _cleanup_tmp_folder(config) + + +async def _cleanup_tmp_folder(config: AgentConfig) -> None: + """ + 清理 executable_code/tmp 文件夹中 3 天前的文件 + + Args: + config: AgentConfig 对象 + """ + try: + if config.project_dir and config.bot_id: + tmp_dir = os.path.join(config.project_dir, "executable_code", "tmp") + if os.path.exists(tmp_dir): + # 3 天前的秒数(3 * 24 * 60 * 60 = 259200) + three_days_ago = time.time() - (3 * 24 * 60 * 60) + + deleted_count = 0 + for item in os.listdir(tmp_dir): + item_path = os.path.join(tmp_dir, item) + # 检查修改时间 + if os.path.getmtime(item_path) < three_days_ago: + if os.path.isfile(item_path) or os.path.islink(item_path): + os.remove(item_path) + else: + shutil.rmtree(item_path) + deleted_count += 1 + logger.debug(f"Deleted old item: {item_path}") + + logger.info(f"Cleaned up {deleted_count} old item(s) from tmp folder: {tmp_dir}") + except Exception as e: + # 清理失败不影响主流程 + logger.error(f"Failed to cleanup tmp folder: {e}") + @router.post("/api/v1/chat/completions") async def chat_completions(request: ChatRequest, authorization: Optional[str] = Header(None)):