Skip to content

Commit

Permalink
fix tool gen
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Oct 22, 2024
1 parent 52ea2d9 commit f75f1c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
43 changes: 20 additions & 23 deletions agentstack/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,23 @@ def add_tools(tools: list, project_name: str):


def list_tools():
try:
# Determine the path to the tools.json file
script_dir = os.path.dirname(os.path.abspath(__file__))
tools_json_path = os.path.join(script_dir, '..', 'tools', 'tools.json')

# Load the JSON data
tools_data = open_json_file(tools_json_path)

# Display the tools
print("\n\nAvailable AgentStack Tools:")
for category, tools in tools_data.items():
print(f"\n{category.capitalize()}:")
for tool in tools:
print(f" - {tool['name']}: {tool['url']}")

print("\n\n❇️ Add a tool with: agentstack tools add <tool_name>")

except FileNotFoundError:
print("Error: tools.json file not found at path:", tools_json_path)
except json.JSONDecodeError:
print("Error: tools.json contains invalid JSON.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
with importlib.resources.path(f'agentstack.tools', 'tools.json') as tools_json_path:
try:
# Load the JSON data
tools_data = open_json_file(tools_json_path)

# Display the tools
print("\n\nAvailable AgentStack Tools:")
for category, tools in tools_data.items():
print(f"\n{category.capitalize()}:")
for tool in tools:
print(f" - {tool['name']}: {tool['url']}")

print("\n\n❇️ Add a tool with: agentstack tools add <tool_name>")

except FileNotFoundError:
print("Error: tools.json file not found at path:", tools_json_path)
except json.JSONDecodeError:
print("Error: tools.json contains invalid JSON.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
44 changes: 24 additions & 20 deletions agentstack/generation/tool_generation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.resources
import json
import sys
from typing import Optional
Expand All @@ -10,33 +11,36 @@


def add_tool(tool_name: str, path: Optional[str] = None):
script_dir = os.path.dirname(os.path.abspath(__file__))
tools = open_json_file(os.path.join(script_dir, '..', 'tools', 'tools.json'))
framework = get_framework(path)
assert_tool_exists(tool_name, tools)
with importlib.resources.path(f'agentstack.tools', 'tools.json') as tools_data_path:
print(tools_data_path)
tools = open_json_file(tools_data_path)
framework = get_framework(path)
assert_tool_exists(tool_name, tools)

tool_data = open_json_file(os.path.join(script_dir, '..', 'tools', f'{tool_name}.json'))
tool_file_route = os.path.join(script_dir, '..', 'templates', framework, 'tools', f'{tool_name}.py')
with importlib.resources.path(f'agentstack.tools', f"{tool_name}.json") as tool_data_path:
tool_data = open_json_file(tool_data_path)

os.system(tool_data['package']) # Install package
shutil.copy(tool_file_route, f'{path or ""}/src/tools/{tool_name}.py') # Move tool from package to project
add_tool_to_tools_init(tool_data, path) # Export tool from tools dir
add_tool_to_agent_definition(framework, tool_data, path)
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
with importlib.resources.path(f'agentstack.templates.{framework}.tools', f"{tool_name}.py") as tool_file_path:
os.system(tool_data['package']) # Install package
shutil.copy(tool_file_path, f'{path + "/" if path else ""}src/tools/{tool_name}.py') # Move tool from package to project
add_tool_to_tools_init(tool_data, path) # Export tool from tools dir
add_tool_to_agent_definition(framework, tool_data, path)
insert_code_after_tag(f'{path + "/" if path else ""}.env', '# Tools', [tool_data['env']], next_line=True) # Add env var
insert_code_after_tag(f'{path + "/" if path else ""}.env.example', '# Tools', [tool_data['env']], next_line=True) # Add env var

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)
agentstack_json = open_json_file(f'{path + "/" if path else ""}agentstack.json')
if not agentstack_json.get('tools'):
agentstack_json['tools'] = []
agentstack_json['tools'].append(tool_name)

print(term_color(f'🔨 Tool {tool_name} added to agentstack project successfully', 'green'))
with open(f'{path + "/" if path else ""}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):
file_path = f'{path or ""}/src/tools/__init__.py'
file_path = f'{path + "/" if path else ""}src/tools/__init__.py'
tag = '# tool import'
code_to_insert = [
f"from {tool_data['name']} import {', '.join([tool_name for tool_name in tool_data['tools']])}"
Expand Down
1 change: 1 addition & 0 deletions agentstack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def main():
generate_parser.print_help()
elif args.command in ['tools', 't']:
if args.tools_command in ['list', 'l']:
print('list')
list_tools()
elif args.tools_command in ['add', 'a']:
generation.add_tool(args.name)
Expand Down

0 comments on commit f75f1c5

Please sign in to comment.