Skip to content

Commit

Permalink
merge with dev
Browse files Browse the repository at this point in the history
  • Loading branch information
namansleeps2 committed Jul 13, 2023
2 parents a25a530 + 06f513a commit 39e7af0
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 49 deletions.
6 changes: 5 additions & 1 deletion config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,8 @@ ENGINE_ID: "stable-diffusion-xl-beta-v2-2-2"

## To use chroma for vector store in resources manager
#CHROMA_HOST_NAME: YOUR_CHROMA_HOST_NAME
#CHROMA_PORT: YOUR_CHROMA_PORT
#CHROMA_PORT: YOUR_CHROMA_PORT

## To use Qdrant for vector store
#QDRANT_HOST_NAME: YOUR_QDRANT_HOST_NAME
#QDRANT_PORT: YOUR_QDRANT_PORT
23 changes: 8 additions & 15 deletions gui/pages/Content/Agents/ResourceList.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useMemo } from 'react';
import styles from './Agents.module.css';
import Image from "next/image";
import { downloadFile, downloadAllFiles, formatBytes, returnResourceIcon } from "@/utils/utils";

export default function ResourceList({ files, channel, runs }) {

const [selectedRun, setSelectedRun] = useState(null);
const [filesByRun, setFilesByRun] = useState([]);

useEffect(() => {
const filesGroupedByRun = runs.map(run => {
const relatedFiles = files.filter(file => file.agent_execution_id === run.id);
return relatedFiles.length !== 0 && { "run": run, "files": relatedFiles };
}).filter(Boolean);

setFilesByRun(filesGroupedByRun);
}, [files, runs]);
const filesByRun = useMemo(() => runs.map(run => {
const relatedFiles = files.filter(file => file.agent_execution_id === run.id);
return relatedFiles.length !== 0 && { "run": run, "files": relatedFiles };
}).filter(Boolean), [files, runs]);

const downloadRunFiles = (run_id) => {
const downloadRunFiles = (run_id,name) => {
const runFiles = files.filter(file => file.agent_execution_id === run_id);
runFiles.length !== 0 && downloadAllFiles(runFiles);
runFiles.length !== 0 && downloadAllFiles(runFiles,name);
}

const isAnyFileWithAgentId = files.some(file => file.agent_execution_id !== null)
Expand Down Expand Up @@ -53,7 +46,7 @@ export default function ResourceList({ files, channel, runs }) {
<span className="text_12 ml_8">{filesRun.run.name}</span>
<div className="resource_manager_tip ml_8"><Image src="/images/bolt.svg" alt="bolt" width={10} height={10} /> <span className="text_9">Run {index + 1}</span></div>
</div>
<Image src="/images/download.svg" alt="download_icon" width={16} height={16} onClick={() => downloadRunFiles(filesRun.run.id)} />
<Image src="/images/download.svg" alt="download_icon" width={16} height={16} onClick={() => downloadRunFiles(filesRun.run.id,filesRun.run.name)} />
</div>

{selectedRun === filesRun.run && (
Expand Down
35 changes: 18 additions & 17 deletions gui/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const downloadFile = (fileId, fileName = null) => {
Authorization: `Bearer ${authToken}`,
};

return fetch(url, {headers})
return fetch(url, { headers })
.then((response) => response.blob())
.then((blob) => {
if (fileName) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export const downloadFile = (fileId, fileName = null) => {
}
};

export const downloadAllFiles = (files) => {
export const downloadAllFiles = (files,run_name) => {
const zip = new JSZip();
const promises = [];
const fileNamesCount = {};
Expand Down Expand Up @@ -131,20 +131,21 @@ export const downloadAllFiles = (files) => {
});

Promise.all(promises)
.then(() => {
zip.generateAsync({type: "blob"})
.then((content) => {
const timestamp = new Date().getTime();
const zipFilename = `files_${timestamp}.zip`;
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(content);
downloadLink.download = zipFilename;
downloadLink.click();
})
.catch((error) => {
console.error("Error generating zip:", error);
});
});
.then(() => {
zip.generateAsync({ type: "blob" })
.then((content) => {
const now = new Date();
const timestamp = `${now.getFullYear()}-${("0" + (now.getMonth() + 1)).slice(-2)}-${("0" + now.getDate()).slice(-2)}_${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`.replace(/:/g, '-');
const zipFilename = `${run_name}_${timestamp}.zip`;
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(content);
downloadLink.download = zipFilename;
downloadLink.click();
})
.catch((error) => {
console.error("Error generating zip:", error);
});
});
};

export const refreshUrl = () => {
Expand Down Expand Up @@ -332,7 +333,7 @@ export const returnResourceIcon = (file) => {
};

export const convertToTitleCase = (str) => {
if (str === null || str === '') {
if(str === null || str === '') {
return '';
}

Expand Down
4 changes: 3 additions & 1 deletion superagi/helper/resource_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def make_written_file_resource(cls, file_name: str, agent: Agent, agent_executio
file_path = ResourceHelper.get_agent_write_resource_path(file_name, agent, agent_execution)

logger.info(final_path)
resource = Resource(name=file_name, path="resources" + file_path, storage_type=storage_type.value,
if StorageType.get_storage_type(get_config("STORAGE_TYPE")) == StorageType.S3:
file_path = "resources" + file_path
resource = Resource(name=file_name, path=file_path, storage_type=storage_type.value,
size=file_size,
type=file_type,
channel="OUTPUT",
Expand Down
14 changes: 8 additions & 6 deletions superagi/tools/file/append_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic import BaseModel, Field

from superagi.helper.resource_helper import ResourceHelper
from superagi.models.agent_execution import AgentExecution
from superagi.tools.base_tool import BaseTool
from superagi.models.agent import Agent

Expand All @@ -26,6 +27,7 @@ class AppendFileTool(BaseTool):
"""
name: str = "Append File"
agent_id: int = None
agent_execution_id: int = None
args_schema: Type[BaseModel] = AppendFileInput
description: str = "Append text to a file"

Expand All @@ -40,12 +42,12 @@ def _execute(self, file_name: str, content: str):
Returns:
success or error message.
"""
final_path = ResourceHelper.get_root_output_dir() + file_name
if "{agent_id}" in final_path:
final_path = ResourceHelper.get_formatted_agent_level_path(agent=Agent
.get_agent_from_id(session=self.toolkit_config.session,
agent_id=self.agent_id),
path=final_path)
final_path = ResourceHelper.get_agent_write_resource_path(file_name, Agent.get_agent_from_id(
session=self.toolkit_config.session,
agent_id=self.agent_id),
AgentExecution.get_agent_execution_from_id(
session=self.toolkit_config.session,
agent_execution_id=self.agent_execution_id))
try:
directory = os.path.dirname(final_path)
os.makedirs(directory, exist_ok=True)
Expand Down
12 changes: 9 additions & 3 deletions superagi/tools/file/delete_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pydantic import BaseModel, Field

from superagi.helper.resource_helper import ResourceHelper
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent import Agent
from superagi.tools.base_tool import BaseTool


Expand All @@ -24,6 +26,7 @@ class DeleteFileTool(BaseTool):
"""
name: str = "Delete File"
agent_id: int = None
agent_execution_id:int = None
args_schema: Type[BaseModel] = DeleteFileInput
description: str = "Delete a file"

Expand All @@ -37,9 +40,12 @@ def _execute(self, file_name: str):
Returns:
success or error message.
"""
final_path = ResourceHelper.get_root_output_dir()
if "{agent_id}" in final_path:
final_path = final_path.replace("{agent_id}", str(self.agent_id))
final_path = ResourceHelper.get_agent_write_resource_path(file_name, Agent.get_agent_from_id(
session=self.toolkit_config.session,
agent_id=self.agent_id),
AgentExecution.get_agent_execution_from_id(
session=self.toolkit_config.session,
agent_execution_id=self.agent_execution_id))
try:
os.remove(final_path + file_name)
return "File deleted successfully."
Expand Down
Loading

0 comments on commit 39e7af0

Please sign in to comment.