Merge branch 'mannaandpoem:main' into main
This commit is contained in:
commit
2a5fa9727f
@ -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)}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user