place the generated file in the workspace directory

This commit is contained in:
Wuzi 2025-03-07 14:40:03 +08:00
parent 612fb4964b
commit 1086a9788a

View File

@ -4,6 +4,7 @@ import os
import aiofiles
from app.tool.base import BaseTool
from app.config import WORKSPACE_ROOT
class FileSaver(BaseTool):
@ -46,16 +47,23 @@ 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)}"