mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-02 07:42:40 +00:00
Fixed File Writer tool errors for better interaction with the agents
This commit is contained in:
@@ -1,19 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
from typing import Any, Optional, Type
|
from typing import Any, Type
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from crewai_tools import BaseTool
|
||||||
from ..base_tool import BaseTool
|
from distutils.util import strtobool
|
||||||
|
|
||||||
|
|
||||||
class FileWriterToolInput(BaseModel):
|
class FileWriterToolInput(BaseModel):
|
||||||
filename: str
|
filename: str
|
||||||
content: str
|
content: str
|
||||||
directory: Optional[str] = None
|
directory: str = "./"
|
||||||
overwrite: bool = False
|
overwrite: str = "False"
|
||||||
|
|
||||||
|
class FileWriterTool2(BaseTool):
|
||||||
class FileWriterTool(BaseTool):
|
|
||||||
name: str = "File Writer Tool"
|
name: str = "File Writer Tool"
|
||||||
description: str = (
|
description: str = (
|
||||||
"A tool to write content to a specified file. Accepts filename, content, and optionally a directory path and overwrite flag as input."
|
"A tool to write content to a specified file. Accepts filename, content, and optionally a directory path and overwrite flag as input."
|
||||||
@@ -23,11 +20,14 @@ class FileWriterTool(BaseTool):
|
|||||||
def _run(self, **kwargs: Any) -> str:
|
def _run(self, **kwargs: Any) -> str:
|
||||||
try:
|
try:
|
||||||
# Create the directory if it doesn't exist
|
# Create the directory if it doesn't exist
|
||||||
if kwargs["directory"] and not os.path.exists(kwargs["directory"]):
|
if kwargs.get("directory") and not os.path.exists(kwargs["directory"]):
|
||||||
os.makedirs(kwargs["directory"])
|
os.makedirs(kwargs["directory"])
|
||||||
|
|
||||||
# Construct the full path
|
# Construct the full path
|
||||||
filepath = os.path.join(kwargs["directory"] or "", kwargs["filename"])
|
filepath = os.path.join(kwargs.get("directory") or "", kwargs["filename"])
|
||||||
|
|
||||||
|
# Convert overwrite to boolean
|
||||||
|
kwargs["overwrite"] = bool(strtobool(kwargs["overwrite"]))
|
||||||
|
|
||||||
# Check if file exists and overwrite is not allowed
|
# Check if file exists and overwrite is not allowed
|
||||||
if os.path.exists(filepath) and not kwargs["overwrite"]:
|
if os.path.exists(filepath) and not kwargs["overwrite"]:
|
||||||
@@ -42,5 +42,7 @@ class FileWriterTool(BaseTool):
|
|||||||
return (
|
return (
|
||||||
f"File {filepath} already exists and overwrite option was not passed."
|
f"File {filepath} already exists and overwrite option was not passed."
|
||||||
)
|
)
|
||||||
|
except KeyError as e:
|
||||||
|
return f"An error occurred while accessing key: {str(e)}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"An error occurred while writing to the file: {str(e)}"
|
return f"An error occurred while writing to the file: {str(e)}"
|
||||||
Reference in New Issue
Block a user