-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rollback functionality to setup wizard #75
Open
HYPRGK
wants to merge
1
commit into
AgentOps-AI:main
Choose a base branch
from
HYPRGK:add-rollback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,86 +20,100 @@ | |
from .. import generation | ||
from ..utils import open_json_file, term_color, is_snake_case | ||
|
||
created_files = [] | ||
created_dirs = [] | ||
|
||
def init_project_builder(slug_name: Optional[str] = None, template: Optional[str] = None, use_wizard: bool = False): | ||
if slug_name and not is_snake_case(slug_name): | ||
print(term_color("Project name must be snake case", 'red')) | ||
return | ||
def rollback_actions(): | ||
for file in created_files: | ||
if os.path.exists(file): | ||
os.remove(file) | ||
for dir in created_dirs: | ||
if os.path.exists(dir): | ||
shutil.rmtree(dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be very careful about deleting files when any exception occurs. Possible that running this command on an already initialized project would permanently erase user data. |
||
|
||
if template is not None and use_wizard: | ||
print(term_color("Template and wizard flags cannot be used together", 'red')) | ||
return | ||
|
||
template_data = None | ||
if template is not None: | ||
url_start = "https://" | ||
if template[:len(url_start)] == url_start: | ||
# template is a url | ||
response = requests.get(template) | ||
if response.status_code == 200: | ||
template_data = response.json() | ||
else: | ||
print(term_color(f"Failed to fetch template data from {template}. Status code: {response.status_code}", 'red')) | ||
sys.exit(1) | ||
else: | ||
with importlib.resources.path('agentstack.templates.proj_templates', f'{template}.json') as template_path: | ||
if template_path is None: | ||
print(term_color(f"No such template {template} found", 'red')) | ||
def init_project_builder(slug_name: Optional[str] = None, template: Optional[str] = None, use_wizard: bool = False): | ||
try: | ||
if slug_name and not is_snake_case(slug_name): | ||
print(term_color("Project name must be snake case", 'red')) | ||
return | ||
|
||
if template is not None and use_wizard: | ||
print(term_color("Template and wizard flags cannot be used together", 'red')) | ||
return | ||
|
||
template_data = None | ||
if template is not None: | ||
url_start = "https://" | ||
if template[:len(url_start)] == url_start: | ||
# template is a url | ||
response = requests.get(template) | ||
if response.status_code == 200: | ||
template_data = response.json() | ||
else: | ||
print(term_color(f"Failed to fetch template data from {template}. Status code: {response.status_code}", 'red')) | ||
sys.exit(1) | ||
template_data = open_json_file(template_path) | ||
|
||
if template_data: | ||
project_details = { | ||
"name": slug_name or template_data['name'], | ||
"version": "0.0.1", | ||
"description": template_data['description'], | ||
"author": "Name <Email>", | ||
"license": "MIT" | ||
} | ||
framework = template_data['framework'] | ||
design = { | ||
'agents': template_data['agents'], | ||
'tasks': template_data['tasks'] | ||
} | ||
|
||
tools = template_data['tools'] | ||
|
||
elif use_wizard: | ||
welcome_message() | ||
project_details = ask_project_details(slug_name) | ||
welcome_message() | ||
framework = ask_framework() | ||
design = ask_design() | ||
tools = ask_tools() | ||
|
||
else: | ||
welcome_message() | ||
project_details = { | ||
"name": slug_name or "agentstack_project", | ||
"version": "0.0.1", | ||
"description": "New agentstack project", | ||
"author": "Name <Email>", | ||
"license": "MIT" | ||
} | ||
|
||
framework = "CrewAI" # TODO: if --no-wizard, require a framework flag | ||
|
||
design = { | ||
'agents': [], | ||
'tasks': [] | ||
} | ||
|
||
tools = [] | ||
|
||
log.debug( | ||
f"project_details: {project_details}" | ||
f"framework: {framework}" | ||
f"design: {design}" | ||
) | ||
insert_template(project_details, framework, design, template_data) | ||
for tool_data in tools: | ||
generation.add_tool(tool_data['name'], agents=tool_data['agents'], path=project_details['name']) | ||
else: | ||
with importlib.resources.path('agentstack.templates.proj_templates', f'{template}.json') as template_path: | ||
if template_path is None: | ||
print(term_color(f"No such template {template} found", 'red')) | ||
sys.exit(1) | ||
template_data = open_json_file(template_path) | ||
|
||
if template_data: | ||
project_details = { | ||
"name": slug_name or template_data['name'], | ||
"version": "0.0.1", | ||
"description": template_data['description'], | ||
"author": "Name <Email>", | ||
"license": "MIT" | ||
} | ||
framework = template_data['framework'] | ||
design = { | ||
'agents': template_data['agents'], | ||
'tasks': template_data['tasks'] | ||
} | ||
|
||
tools = template_data['tools'] | ||
|
||
elif use_wizard: | ||
welcome_message() | ||
project_details = ask_project_details(slug_name) | ||
welcome_message() | ||
framework = ask_framework() | ||
design = ask_design() | ||
tools = ask_tools() | ||
|
||
else: | ||
welcome_message() | ||
project_details = { | ||
"name": slug_name or "agentstack_project", | ||
"version": "0.0.1", | ||
"description": "New agentstack project", | ||
"author": "Name <Email>", | ||
"license": "MIT" | ||
} | ||
|
||
framework = "CrewAI" # TODO: if --no-wizard, require a framework flag | ||
|
||
design = { | ||
'agents': [], | ||
'tasks': [] | ||
} | ||
|
||
tools = [] | ||
|
||
log.debug( | ||
f"project_details: {project_details}" | ||
f"framework: {framework}" | ||
f"design: {design}" | ||
) | ||
insert_template(project_details, framework, design, template_data) | ||
for tool_data in tools: | ||
generation.add_tool(tool_data['name'], agents=tool_data['agents'], path=project_details['name']) | ||
except Exception as e: | ||
print(term_color(f"An error occurred: {e}", 'red')) | ||
rollback_actions() | ||
sys.exit(1) | ||
|
||
def welcome_message(): | ||
os.system("cls" if os.name == "nt" else "clear") | ||
|
@@ -323,17 +337,20 @@ def insert_template(project_details: dict, framework_name: str, design: dict, te | |
template_path = get_package_path() / f'templates/{framework.name}' | ||
with open(f"{template_path}/cookiecutter.json", "w") as json_file: | ||
json.dump(cookiecutter_data.to_dict(), json_file) | ||
created_files.append(f"{template_path}/cookiecutter.json") | ||
|
||
# copy .env.example to .env | ||
shutil.copy( | ||
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env.example', | ||
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env') | ||
created_files.append(f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env') | ||
|
||
if os.path.isdir(project_details['name']): | ||
print(term_color(f"Directory {template_path} already exists. Please check this and try again", "red")) | ||
return | ||
|
||
cookiecutter(str(template_path), no_input=True, extra_context=None) | ||
created_dirs.append(project_details['name']) | ||
|
||
# TODO: inits a git repo in the directory the command was run in | ||
# TODO: not where the project is generated. Fix this | ||
|
@@ -378,4 +395,4 @@ def list_tools(): | |
print(f": {tool.url if tool.url else 'AgentStack default tool'}") | ||
|
||
print("\n\n✨ Add a tool with: agentstack tools add <tool_name>") | ||
print(" https://docs.agentstack.sh/tools/core") | ||
print(" https://docs.agentstack.sh/tools/core") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These variables should be created inside the function scope, not at the module level.