Skip to content

Commit

Permalink
making agent_id as part of config in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
TransformerOptimus committed Jun 23, 2023
1 parent 6d7bca8 commit cc38c5e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
36 changes: 17 additions & 19 deletions superagi/controllers/resources.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
from fastapi_sqlalchemy import DBSessionMiddleware, db
from fastapi import HTTPException, Depends, Request
from fastapi_jwt_auth import AuthJWT
from fastapi_jwt_auth.exceptions import AuthJWTException
from superagi.models.budget import Budget
from fastapi import APIRouter, UploadFile
from pydantic_sqlalchemy import sqlalchemy_to_pydantic
import datetime
import os
from fastapi import FastAPI, File, Form, UploadFile
from typing import Annotated
from superagi.models.resource import Resource
from superagi.config.config import get_config
from superagi.models.agent import Agent
from starlette.responses import FileResponse
from pathlib import Path
from fastapi.responses import StreamingResponse
from superagi.helper.auth import check_auth

import boto3
import datetime
from botocore.exceptions import NoCredentialsError
import tempfile
import requests
from fastapi import APIRouter
from fastapi import File, Form, UploadFile
from fastapi import HTTPException, Depends
from fastapi.responses import StreamingResponse
from fastapi_jwt_auth import AuthJWT
from fastapi_sqlalchemy import db

from superagi.config.config import get_config
from superagi.helper.auth import check_auth
from superagi.helper.resource_helper import ResourceHelper
from superagi.lib.logger import logger
from superagi.models.agent import Agent
from superagi.models.resource import Resource

router = APIRouter()

Expand Down Expand Up @@ -64,7 +60,9 @@ async def upload(agent_id: int, file: UploadFile = File(...), name=Form(...), si

storage_type = get_config("STORAGE_TYPE")
Resource.validate_resource_type(storage_type)
save_directory = get_config("RESOURCES_INPUT_ROOT_DIR")
save_directory = ResourceHelper.get_root_input_dir() + "/"
if "{agent_id}" in save_directory:
save_directory = save_directory.replace("{agent_id}", str(agent_id))
path = ""
os.makedirs(save_directory, exist_ok=True)
file_path = os.path.join(save_directory, file.filename)
Expand Down
8 changes: 4 additions & 4 deletions superagi/helper/resource_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def make_written_file_resource(file_name: str, agent_id: int, channel: str):

if agent_id is not None:
final_path = ResourceHelper.get_agent_resource_path(file_name, agent_id)
path = path + str(agent_id) + "/"
path = path.replace("{agent_id}", str(agent_id))
else:
final_path = ResourceHelper.get_resource_path(file_name)
file_size = os.path.getsize(final_path)
Expand Down Expand Up @@ -94,9 +94,9 @@ def get_agent_resource_path(file_name: str, agent_id: int):
file_name (str): The name of the file.
"""
root_dir = ResourceHelper.get_root_output_dir()
if agent_id is not None:
directory = os.path.dirname(root_dir + str(agent_id) + "/")
if agent_id is not None and "{agent_id}" in root_dir:
root_dir = root_dir.replace("{agent_id}", str(agent_id))
directory = os.path.dirname(root_dir)
os.makedirs(directory, exist_ok=True)
root_dir = root_dir + str(agent_id) + "/"
final_path = root_dir + file_name
return final_path
4 changes: 3 additions & 1 deletion superagi/tools/file/append_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def _execute(self, file_name: str, content: str):
Returns:
file written to successfully. or error message.
"""
final_path = ResourceHelper.get_root_output_dir() + str(self.agent_id) + "/" + file_name
final_path = ResourceHelper.get_root_output_dir() + file_name
if "{agent_id}" in final_path:
final_path = final_path.replace("{agent_id}", str(self.agent_id))
try:
directory = os.path.dirname(final_path)
os.makedirs(directory, exist_ok=True)
Expand Down
4 changes: 3 additions & 1 deletion superagi/tools/file/delete_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def _execute(self, file_name: str):
Returns:
file deleted successfully. or error message.
"""
final_path = ResourceHelper.get_root_output_dir() + str(self.agent_id) + "/"
final_path = ResourceHelper.get_root_output_dir()
if "{agent_id}" in final_path:
final_path = final_path.replace("{agent_id}", str(self.agent_id))
try:
os.remove(final_path + file_name)
return "File deleted successfully."
Expand Down
10 changes: 8 additions & 2 deletions superagi/tools/file/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def _execute(self):
Returns:
list of files in directory.
"""
input_files = self.list_files(ResourceHelper.get_root_input_dir() + str(self.agent_id) + "/")
output_files = self.list_files(ResourceHelper.get_root_output_dir() + str(self.agent_id) + "/")
input_directory = ResourceHelper.get_root_input_dir() + str(self.agent_id) + "/"
output_directory = ResourceHelper.get_root_output_dir() + str(self.agent_id) + "/"
if "{agent_id}" in input_directory:
input_directory = input_directory.replace("{agent_id}", str(self.agent_id))
if "{agent_id}" in output_directory:
output_directory = output_directory.replace("{agent_id}", str(self.agent_id))
input_files = self.list_files(input_directory)
output_files = self.list_files(output_directory)
return input_files + output_files

def list_files(self, directory):
Expand Down
13 changes: 10 additions & 3 deletions superagi/tools/file/read_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ def _execute(self, file_name: str):
"""
output_root_dir = ResourceHelper.get_root_output_dir()

final_path = ResourceHelper.get_root_input_dir() + str(self.agent_id) + "/" + file_name
final_path = ResourceHelper.get_root_input_dir() + file_name
if "{agent_id}" in final_path:
final_path = final_path.replace("{agent_id}", str(self.agent_id))

if final_path is None or not os.path.exists(final_path):
if output_root_dir is not None:
final_path = ResourceHelper.get_root_output_dir() + str(self.agent_id) + "/" + file_name
final_path = ResourceHelper.get_root_output_dir() + file_name
if "{agent_id}" in final_path:
final_path = final_path.replace("{agent_id}", str(self.agent_id))

if final_path is None or not os.path.exists(final_path):
raise FileNotFoundError(f"File '{file_name}' not found.")
Expand All @@ -54,4 +58,7 @@ def _execute(self, file_name: str):

with open(final_path, 'r') as file:
file_content = file.read()
return file_content[:1500]
max_length = len(' '.join(file_content.split(" ")[:1200]))
return file_content[:max_length]


4 changes: 2 additions & 2 deletions tests/unit_tests/helper/test_resource_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_make_written_file_resource(mocker):
result = ResourceHelper.make_written_file_resource('test.txt', 1, 'INPUT')

assert result.name == 'test.txt'
assert result.path == '/1/test.txt'
assert result.path == '/test.txt'
assert result.storage_type == 'local'
assert result.size == 1000
assert result.type == 'application/txt'
Expand All @@ -36,4 +36,4 @@ def test_get_agent_resource_path(mocker):

result = ResourceHelper.get_agent_resource_path('test.txt', 1)

assert result == '/1/test.txt'
assert result == '/test.txt'

0 comments on commit cc38c5e

Please sign in to comment.