From 19635d80f6c45e0bd101f8691a0830c440486e70 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 Jan 2025 19:54:30 +0000 Subject: [PATCH 1/4] resolves #175 --- agentstack/cli/run.py | 2 ++ agentstack/conf.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/agentstack/cli/run.py b/agentstack/cli/run.py index 17c48b4..4e76764 100644 --- a/agentstack/cli/run.py +++ b/agentstack/cli/run.py @@ -6,6 +6,7 @@ from dotenv import load_dotenv from agentstack import conf +from agentstack.conf import assert_project from agentstack.exceptions import ValidationError from agentstack import inputs from agentstack import frameworks @@ -91,6 +92,7 @@ def _import_project_module(path: Path): def run_project(command: str = 'run', debug: bool = False, cli_args: Optional[str] = None): """Validate that the project is ready to run and then run it.""" + assert_project() if conf.get_framework() not in frameworks.SUPPORTED_FRAMEWORKS: print(term_color(f"Framework {conf.get_framework()} is not supported by agentstack.", 'red')) sys.exit(1) diff --git a/agentstack/conf.py b/agentstack/conf.py index 2b7810e..a91b87e 100644 --- a/agentstack/conf.py +++ b/agentstack/conf.py @@ -3,6 +3,8 @@ import json from pathlib import Path from pydantic import BaseModel + +from agentstack.exceptions import ValidationError from agentstack.utils import get_version @@ -11,6 +13,12 @@ PATH: Path = Path() +def assert_project() -> None: + try: + ConfigFile() + return + except FileNotFoundError: + raise Exception("Could not find agentstack.json, are you in an AgentStack project directory?") def set_path(path: Union[str, Path, None]): """Set the path to the project directory.""" From 202372ddd88288c3a25e2ff71bcb4e4b4292b4f9 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 Jan 2025 20:07:54 +0000 Subject: [PATCH 2/4] resolves #175 --- agentstack/cli/run.py | 2 -- agentstack/conf.py | 6 +++++- agentstack/main.py | 39 ++++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/agentstack/cli/run.py b/agentstack/cli/run.py index 4e76764..17c48b4 100644 --- a/agentstack/cli/run.py +++ b/agentstack/cli/run.py @@ -6,7 +6,6 @@ from dotenv import load_dotenv from agentstack import conf -from agentstack.conf import assert_project from agentstack.exceptions import ValidationError from agentstack import inputs from agentstack import frameworks @@ -92,7 +91,6 @@ def _import_project_module(path: Path): def run_project(command: str = 'run', debug: bool = False, cli_args: Optional[str] = None): """Validate that the project is ready to run and then run it.""" - assert_project() if conf.get_framework() not in frameworks.SUPPORTED_FRAMEWORKS: print(term_color(f"Framework {conf.get_framework()} is not supported by agentstack.", 'red')) sys.exit(1) diff --git a/agentstack/conf.py b/agentstack/conf.py index a91b87e..a3c5081 100644 --- a/agentstack/conf.py +++ b/agentstack/conf.py @@ -18,7 +18,7 @@ def assert_project() -> None: ConfigFile() return except FileNotFoundError: - raise Exception("Could not find agentstack.json, are you in an AgentStack project directory?") + raise NoProjectException("Could not find agentstack.json, are you in an AgentStack project directory?") def set_path(path: Union[str, Path, None]): """Set the path to the project directory.""" @@ -104,3 +104,7 @@ def __enter__(self) -> 'ConfigFile': def __exit__(self, *args): self.write() + + +class NoProjectException(Exception): + pass diff --git a/agentstack/main.py b/agentstack/main.py index 07e0c97..2472922 100644 --- a/agentstack/main.py +++ b/agentstack/main.py @@ -159,6 +159,7 @@ def main(): # Handle commands try: + # outside of project if args.command in ["docs"]: webbrowser.open("https://docs.agentstack.sh/") elif args.command in ["quickstart"]: @@ -167,36 +168,48 @@ def main(): webbrowser.open("https://docs.agentstack.sh/quickstart") elif args.command in ["init", "i"]: init_project_builder(args.slug_name, args.template, args.wizard) - elif args.command in ["run", "r"]: - run_project(command=args.function, debug=args.debug, cli_args=extra_args) - elif args.command in ['generate', 'g']: - if args.generate_command in ['agent', 'a']: - if not args.llm: - configure_default_model() - generation.add_agent(args.name, args.role, args.goal, args.backstory, args.llm) - elif args.generate_command in ['task', 't']: - generation.add_task(args.name, args.description, args.expected_output, args.agent) - else: - generate_parser.print_help() elif args.command in ["tools", "t"]: if args.tools_command in ["list", "l"]: list_tools() elif args.tools_command in ["add", "a"]: + conf.assert_project() agents = [args.agent] if args.agent else None agents = args.agents.split(",") if args.agents else agents add_tool(args.name, agents) elif args.tools_command in ["remove", "r"]: + conf.assert_project() generation.remove_tool(args.name) else: tools_parser.print_help() - elif args.command in ['export', 'e']: - export_template(args.filename) elif args.command in ['login']: auth.login() elif args.command in ['update', 'u']: pass # Update check already done + + # inside project dir commands only + conf.assert_project() + + if args.command in ["run", "r"]: + run_project(command=args.function, debug=args.debug, cli_args=extra_args) + elif args.command in ['generate', 'g']: + if args.generate_command in ['agent', 'a']: + if not args.llm: + configure_default_model() + generation.add_agent(args.name, args.role, args.goal, args.backstory, args.llm) + elif args.generate_command in ['task', 't']: + generation.add_task(args.name, args.description, args.expected_output, args.agent) + else: + generate_parser.print_help() + elif args.command in ['export', 'e']: + export_template(args.filename) else: parser.print_help() + + except conf.NoProjectException as e: + update_telemetry(telemetry_id, result=1, message=str(e)) + print(term_color(str(e), "red")) + sys.exit(1) + except Exception as e: update_telemetry(telemetry_id, result=1, message=str(e)) print(term_color("An error occurred while running your AgentStack command:", "red")) From cc9c74901c41a183c308041a7094efb5a647daf1 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 Jan 2025 20:10:36 +0000 Subject: [PATCH 3/4] unused imports --- agentstack/conf.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/agentstack/conf.py b/agentstack/conf.py index a3c5081..a6a2585 100644 --- a/agentstack/conf.py +++ b/agentstack/conf.py @@ -1,10 +1,8 @@ from typing import Optional, Union -import os, sys +import os import json from pathlib import Path from pydantic import BaseModel - -from agentstack.exceptions import ValidationError from agentstack.utils import get_version From 51216f96c535431aaa3370dba94c2e572e5479f8 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Mon, 6 Jan 2025 17:25:34 +0000 Subject: [PATCH 4/4] remove custom exception --- agentstack/conf.py | 6 +----- agentstack/main.py | 5 ----- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/agentstack/conf.py b/agentstack/conf.py index a6a2585..c92e49d 100644 --- a/agentstack/conf.py +++ b/agentstack/conf.py @@ -16,7 +16,7 @@ def assert_project() -> None: ConfigFile() return except FileNotFoundError: - raise NoProjectException("Could not find agentstack.json, are you in an AgentStack project directory?") + raise Exception("Could not find agentstack.json, are you in an AgentStack project directory?") def set_path(path: Union[str, Path, None]): """Set the path to the project directory.""" @@ -102,7 +102,3 @@ def __enter__(self) -> 'ConfigFile': def __exit__(self, *args): self.write() - - -class NoProjectException(Exception): - pass diff --git a/agentstack/main.py b/agentstack/main.py index 44b1c88..858ae2d 100644 --- a/agentstack/main.py +++ b/agentstack/main.py @@ -205,11 +205,6 @@ def main(): else: parser.print_help() - except conf.NoProjectException as e: - update_telemetry(telemetry_id, result=1, message=str(e)) - print(term_color(str(e), "red")) - sys.exit(1) - except Exception as e: update_telemetry(telemetry_id, result=1, message=str(e)) print(term_color("An error occurred while running your AgentStack command:", "red"))