|
1 | 1 | import sys |
| 2 | +from typing import Optional |
| 3 | + |
2 | 4 | from .gen_utils import insert_code_after_tag |
3 | 5 | from ..utils import snake_to_camel, open_json_file, get_framework |
4 | 6 | import os |
5 | 7 | import shutil |
6 | 8 | import fileinput |
7 | 9 |
|
8 | 10 |
|
9 | | -def add_tool(tool_name: str): |
| 11 | +def add_tool(tool_name: str, path: Optional[str] = None): |
10 | 12 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
11 | 13 | tools = open_json_file(os.path.join(script_dir, '..', 'tools', 'tools.json')) |
12 | | - framework = get_framework() |
| 14 | + framework = get_framework(path) |
13 | 15 | assert_tool_exists(tool_name, tools) |
14 | 16 |
|
15 | 17 | tool_data = open_json_file(os.path.join(script_dir, '..', 'tools', f'{tool_name}.json')) |
16 | 18 | tool_file_route = os.path.join(script_dir, '..', 'templates', framework, 'tools', f'{tool_name}.py') |
17 | 19 |
|
18 | 20 | os.system(tool_data['package']) # Install package |
19 | | - shutil.copy(tool_file_route, f'src/tools/{tool_name}.py') # Move tool from package to project |
20 | | - add_tool_to_tools_init(tool_data) # Export tool from tools dir |
21 | | - add_tool_to_agent_definition(framework, tool_data) |
22 | | - insert_code_after_tag('.env', '# Tools', [tool_data['env']], next_line=True) # Add env var |
23 | | - insert_code_after_tag('.env.example', '# Tools', [tool_data['env']], next_line=True) # Add env var |
| 21 | + shutil.copy(tool_file_route, f'{path or ""}/src/tools/{tool_name}.py') # Move tool from package to project |
| 22 | + add_tool_to_tools_init(tool_data, path) # Export tool from tools dir |
| 23 | + add_tool_to_agent_definition(framework, tool_data, path) |
| 24 | + insert_code_after_tag(f'{path}/.env', '# Tools', [tool_data['env']], next_line=True) # Add env var |
| 25 | + insert_code_after_tag(f'{path}/.env.example', '# Tools', [tool_data['env']], next_line=True) # Add env var |
24 | 26 |
|
25 | 27 | print(f'\033[92m🔨 Tool {tool_name} added to agentstack project successfully\033[0m') |
26 | 28 |
|
27 | | -def add_tool_to_tools_init(tool_data: dict): |
28 | | - file_path = 'src/tools/__init__.py' |
| 29 | + |
| 30 | +def add_tool_to_tools_init(tool_data: dict, path: Optional[str] = None): |
| 31 | + file_path = f'{path or ""}/src/tools/__init__.py' |
29 | 32 | tag = '# tool import' |
30 | 33 | code_to_insert = [ |
31 | 34 | f"from {tool_data['name']} import {', '.join([tool_name for tool_name in tool_data['tools']])}" |
32 | 35 | ] |
33 | 36 | insert_code_after_tag(file_path, tag, code_to_insert, next_line=True) |
34 | 37 |
|
35 | 38 |
|
36 | | -def add_tool_to_agent_definition(framework: str, tool_data: dict): |
| 39 | +def add_tool_to_agent_definition(framework: str, tool_data: dict, path: Optional[str] = None): |
37 | 40 | filename = '' |
38 | 41 | if framework == 'crewai': |
39 | 42 | filename = 'src/crew.py' |
40 | 43 |
|
| 44 | + if path: |
| 45 | + filename = f'{path}/{filename}' |
| 46 | + |
41 | 47 | with fileinput.input(files=filename, inplace=True) as f: |
42 | 48 | for line in f: |
43 | 49 | print(line.replace('tools=[', f'tools=[tools.{", tools.".join([tool_name for tool_name in tool_data["tools"]])}, '), end='') |
|
0 commit comments