Merge branch 'mannaandpoem:main' into main

This commit is contained in:
Sheng Fan 2025-03-16 19:57:41 +08:00 committed by GitHub
commit 2a5fa9727f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ import os
import aiofiles
from app.tool.base import BaseTool
from app.config import WORKSPACE_ROOT
class FileSaver(BaseTool):
@ -45,15 +46,22 @@ The tool accepts content and a file path, and saves the content to that location
str: A message indicating the result of the operation.
"""
try:
# Place the generated file in the workspace directory
if os.path.isabs(file_path):
file_name = os.path.basename(file_path)
full_path = os.path.join(WORKSPACE_ROOT, file_name)
else:
full_path = os.path.join(WORKSPACE_ROOT, file_path)
# Ensure the directory exists
directory = os.path.dirname(file_path)
directory = os.path.dirname(full_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)
# Write directly to the file
async with aiofiles.open(file_path, mode, encoding="utf-8") as file:
async with aiofiles.open(full_path, mode, encoding="utf-8") as file:
await file.write(content)
return f"Content successfully saved to {file_path}"
return f"Content successfully saved to {full_path}"
except Exception as e:
return f"Error saving file: {str(e)}"