Skip to content

Commit

Permalink
handle artifact bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Occupying-Mars committed Dec 5, 2024
1 parent 82a2d89 commit f74f731
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,36 +2388,42 @@ def handle_artifacts(
) -> None:
"""Handle creating and saving artifacts with error handling."""
try:
logger.info(
f"Creating artifact for file: {file_output_path}"
)
# Ensure file_extension starts with a dot
if not file_extension.startswith('.'):
file_extension = '.' + file_extension

# If file_output_path doesn't have an extension, treat it as a directory
# and create a default filename based on timestamp
if not os.path.splitext(file_output_path)[1]:
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"artifact_{timestamp}{file_extension}"
full_path = os.path.join(file_output_path, filename)
else:
full_path = file_output_path

# Create the directory if it doesn't exist
os.makedirs(os.path.dirname(full_path), exist_ok=True)

logger.info(f"Creating artifact for file: {full_path}")
artifact = Artifact(
file_path=file_output_path,
file_path=full_path,
file_type=file_extension,
contents=text,
edit_count=0,
)

logger.info(
f"Saving artifact with extension: {file_extension}"
)
logger.info(f"Saving artifact with extension: {file_extension}")
artifact.save_as(file_extension)
logger.success(
f"Successfully saved artifact to {file_output_path}"
)
logger.success(f"Successfully saved artifact to {full_path}")

except ValueError as e:
logger.error(
f"Invalid input values for artifact: {str(e)}"
)
logger.error(f"Invalid input values for artifact: {str(e)}")
raise
except IOError as e:
logger.error(f"Error saving artifact to file: {str(e)}")
raise
except Exception as e:
logger.error(
f"Unexpected error handling artifact: {str(e)}"
)
logger.error(f"Unexpected error handling artifact: {str(e)}")
raise

def showcase_config(self):
Expand Down

0 comments on commit f74f731

Please sign in to comment.