-
Notifications
You must be signed in to change notification settings - Fork 192
Validate framework entrypoint file before execution #78
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09f1787
Validate framework entrypoint file before execution. (#60)
tcdent 8175251
Recursive tmp directory creation in CLI tests.
tcdent 3da6973
Add better error messaging to crew validation.
tcdent d39596f
Merge branch 'main' into issue-60
tcdent 7e71b2a
Merge branch 'main' into issue-60
tcdent 3cf5aaf
Merge branch 'main' into issue-60
tcdent 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 hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| from .cli import init_project_builder, list_tools, configure_default_model | ||
| from .cli import init_project_builder, list_tools, configure_default_model, run_project |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """ | ||
| Methods for interacting with framework-specific features. | ||
|
|
||
| Each framework should have a module in the `frameworks` package which defines the following methods: | ||
|
|
||
| - `ENTRYPOINT`: Path: Relative path to the entrypoint file for the framework | ||
| - `validate_project(path: Optional[Path] = None) -> None`: Validate that a project is ready to run. | ||
| Raises a `ValidationError` if the project is not valid. | ||
| """ | ||
| from typing import Optional | ||
| from importlib import import_module | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| CREWAI = 'crewai' | ||
| SUPPORTED_FRAMEWORKS = [CREWAI, ] | ||
|
|
||
| def get_framework_module(framework: str) -> import_module: | ||
| """ | ||
| Get the module for a framework. | ||
| """ | ||
| if framework == CREWAI: | ||
| from . import crewai | ||
| return crewai | ||
| else: | ||
| raise ValueError(f"Framework {framework} not supported") | ||
|
|
||
| def get_entrypoint_path(framework: str) -> Path: | ||
| """ | ||
| Get the path to the entrypoint file for a framework. | ||
| """ | ||
| return get_framework_module(framework).ENTRYPOINT | ||
|
|
||
| class ValidationError(Exception): pass | ||
|
|
||
| def validate_project(framework: str, path: Optional[Path] = None) -> None: | ||
| """ | ||
| Run the framework specific project validation. | ||
| """ | ||
| return get_framework_module(framework).validate_project(path) | ||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from typing import Optional | ||
| from pathlib import Path | ||
| import ast | ||
| from . import SUPPORTED_FRAMEWORKS, ValidationError | ||
|
|
||
|
|
||
| ENTRYPOINT: Path = Path('src/crew.py') | ||
|
|
||
| def validate_project(path: Optional[Path] = None) -> None: | ||
| """ | ||
| Validate that a CrewAI project is ready to run. | ||
| Raises a frameworks.VaidationError if the project is not valid. | ||
| """ | ||
| try: | ||
| if path is None: path = Path() | ||
| with open(path/ENTRYPOINT, 'r') as f: | ||
| tree = ast.parse(f.read()) | ||
| except (FileNotFoundError, SyntaxError) as e: | ||
| raise ValidationError(f"Failed to parse {ENTRYPOINT}\n {e}") | ||
|
|
||
| # A valid project must have a class in the crew.py file decorated with `@CrewBase` | ||
| try: | ||
| class_node = _find_class_with_decorator(tree, 'CrewBase')[0] | ||
| except IndexError: | ||
| raise ValidationError(f"`@CrewBase` decorated class not found in {ENTRYPOINT}") | ||
|
|
||
| # The Crew class must have one or more methods decorated with `@agent` | ||
| if len(_find_decorated_method_in_class(class_node, 'task')) < 1: | ||
| raise ValidationError( | ||
| f"`@task` decorated method not found in `{class_node.name}` class in {ENTRYPOINT}.\n" | ||
| "Create a new task using `agentstack generate task <task_name>`.") | ||
|
|
||
| # The Crew class must have one or more methods decorated with `@agent` | ||
| if len(_find_decorated_method_in_class(class_node, 'agent')) < 1: | ||
| raise ValidationError( | ||
| f"`@agent` decorated method not found in `{class_node.name}` class in {ENTRYPOINT}.\n" | ||
| "Create a new agent using `agentstack generate agent <agent_name>`.") | ||
|
|
||
| # The Crew class must have one method decorated with `@crew` | ||
| if len(_find_decorated_method_in_class(class_node, 'crew')) < 1: | ||
| raise ValidationError(f"`@crew` decorated method not found in `{class_node.name}` class in {ENTRYPOINT}") | ||
|
|
||
| # TODO move these to a shared AST utility module | ||
tcdent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def _find_class_with_decorator(tree: ast.AST, decorator_name: str) -> list[ast.ClassDef]: | ||
| """Find a class definition that is marked by a decorator in an AST.""" | ||
| nodes = [] | ||
| for node in ast.iter_child_nodes(tree): | ||
| if isinstance(node, ast.ClassDef): | ||
| for decorator in node.decorator_list: | ||
| if isinstance(decorator, ast.Name) and decorator.id == decorator_name: | ||
| nodes.append(node) | ||
| return nodes | ||
|
|
||
| def _find_decorated_method_in_class(classdef: ast.ClassDef, decorator_name: str) -> list[ast.FunctionDef]: | ||
| """Find all method definitions in a class definition which are decorated with a specific decorator.""" | ||
| nodes = [] | ||
| for node in ast.iter_child_nodes(classdef): | ||
| if isinstance(node, ast.FunctionDef): | ||
| for decorator in node.decorator_list: | ||
| if isinstance(decorator, ast.Name) and decorator.id == decorator_name: | ||
| nodes.append(node) | ||
| return nodes | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "framework": "crewai", | ||
| "tools": ["tool1", "tool2"] | ||
| "tools": [] | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.