From 2f67a0789a50ed2fa88b02d72b28783552023bda Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Wed, 16 Oct 2024 23:15:07 -0700 Subject: [PATCH] add tool to agenstack.json --- agentstack/generation/tool_generation.py | 12 ++++++++++-- agentstack/utils.py | 12 ++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/agentstack/generation/tool_generation.py b/agentstack/generation/tool_generation.py index 76927dd..2df7c9c 100644 --- a/agentstack/generation/tool_generation.py +++ b/agentstack/generation/tool_generation.py @@ -1,8 +1,9 @@ +import json import sys from typing import Optional from .gen_utils import insert_code_after_tag -from ..utils import snake_to_camel, open_json_file, get_framework +from ..utils import open_json_file, get_framework, term_color import os import shutil import fileinput @@ -24,7 +25,14 @@ def add_tool(tool_name: str, path: Optional[str] = None): insert_code_after_tag(f'{path}/.env', '# Tools', [tool_data['env']], next_line=True) # Add env var insert_code_after_tag(f'{path}/.env.example', '# Tools', [tool_data['env']], next_line=True) # Add env var - print(f'\033[92m🔨 Tool {tool_name} added to agentstack project successfully\033[0m') + agentstack_json = open_json_file(f'{path}/agentstack.json') + if not agentstack_json.get('tools'): + agentstack_json['tools'] = [] + agentstack_json['tools'].append(tool_name) + with open(f'{path}/agentstack.json', 'w') as f: + json.dump(agentstack_json, f, indent=4) + + print(term_color(f'🔨 Tool {tool_name} added to agentstack project successfully', 'green')) def add_tool_to_tools_init(tool_data: dict, path: Optional[str] = None): diff --git a/agentstack/utils.py b/agentstack/utils.py index 809aa7a..d9309b8 100644 --- a/agentstack/utils.py +++ b/agentstack/utils.py @@ -30,14 +30,14 @@ def get_framework(path: Optional[str] = None) -> str: file_path = 'agentstack.json' if path is not None: file_path = path + '/' + file_path - with open(file_path, 'r') as f: - data = json.load(f) - framework = data.get('framework') - if framework.lower() not in ['crewai', 'autogen', 'litellm']: - print("\033[31magentstack.json contains an invalid framework\033[0m") + agentstack_data = open_json_file(file_path) + framework = agentstack_data.get('framework') - return framework + if framework.lower() not in ['crewai', 'autogen', 'litellm']: + print(term_color("agentstack.json contains an invalid framework", "red")) + + return framework except FileNotFoundError: print("\033[31mFile agentstack.json does not exist. Are you in the right directory?\033[0m") sys.exit(1)