|
| 1 | +from typing import Optional, Protocol |
| 2 | +from types import ModuleType |
| 3 | +from importlib import import_module |
| 4 | +from pathlib import Path |
| 5 | +from agentstack import ValidationError |
| 6 | +from agentstack.tools import ToolConfig |
| 7 | +from agentstack.agents import AgentConfig |
| 8 | +from agentstack.tasks import TaskConfig |
| 9 | + |
| 10 | + |
| 11 | +CREWAI = 'crewai' |
| 12 | +SUPPORTED_FRAMEWORKS = [CREWAI, ] |
| 13 | + |
| 14 | +class FrameworkModule(Protocol): |
| 15 | + """ |
| 16 | + Protocol spec for a framework implementation module. |
| 17 | + """ |
| 18 | + ENTRYPOINT: Path |
| 19 | + """ |
| 20 | + Relative path to the entrypoint file for the framework in the user's project. |
| 21 | + ie. `src/crewai.py` |
| 22 | + """ |
| 23 | + |
| 24 | + def validate_project(self, path: Optional[Path] = None) -> None: |
| 25 | + """ |
| 26 | + Validate that a user's project is ready to run. |
| 27 | + Raises a `ValidationError` if the project is not valid. |
| 28 | + """ |
| 29 | + ... |
| 30 | + |
| 31 | + def add_tool(self, tool: ToolConfig, agent_name: str, path: Optional[Path] = None) -> None: |
| 32 | + """ |
| 33 | + Add a tool to an agent in the user's project. |
| 34 | + """ |
| 35 | + ... |
| 36 | + |
| 37 | + def remove_tool(self, tool: ToolConfig, agent_name: str, path: Optional[Path] = None) -> None: |
| 38 | + """ |
| 39 | + Remove a tool from an agent in user's project. |
| 40 | + """ |
| 41 | + ... |
| 42 | + |
| 43 | + def get_agent_names(self, path: Optional[Path] = None) -> list[str]: |
| 44 | + """ |
| 45 | + Get a list of agent names in the user's project. |
| 46 | + """ |
| 47 | + ... |
| 48 | + |
| 49 | + def add_agent(self, agent: AgentConfig, path: Optional[Path] = None) -> None: |
| 50 | + """ |
| 51 | + Add an agent to the user's project. |
| 52 | + """ |
| 53 | + ... |
| 54 | + |
| 55 | + def add_task(self, task: TaskConfig, path: Optional[Path] = None) -> None: |
| 56 | + """ |
| 57 | + Add a task to the user's project. |
| 58 | + """ |
| 59 | + ... |
| 60 | + |
| 61 | + |
| 62 | +def get_framework_module(framework: str) -> FrameworkModule: |
| 63 | + """ |
| 64 | + Get the module for a framework. |
| 65 | + """ |
| 66 | + try: |
| 67 | + return import_module(f".{framework}", package=__package__) |
| 68 | + except ImportError: |
| 69 | + raise Exception(f"Framework {framework} could not be imported.") |
| 70 | + |
| 71 | +def get_entrypoint_path(framework: str, path: Optional[Path] = None) -> Path: |
| 72 | + """ |
| 73 | + Get the path to the entrypoint file for a framework. |
| 74 | + """ |
| 75 | + if path is None: |
| 76 | + path = Path() |
| 77 | + return path / get_framework_module(framework).ENTRYPOINT |
| 78 | + |
| 79 | +def validate_project(framework: str, path: Optional[Path] = None): |
| 80 | + """ |
| 81 | + Validate that the user's project is ready to run. |
| 82 | + """ |
| 83 | + return get_framework_module(framework).validate_project(path) |
| 84 | + |
| 85 | +def add_tool(framework: str, tool: ToolConfig, agent_name: str, path: Optional[Path] = None): |
| 86 | + """ |
| 87 | + Add a tool to the user's project. |
| 88 | + The tool will have aready been installed in the user's application and have |
| 89 | + all dependencies installed. We're just handling code generation here. |
| 90 | + """ |
| 91 | + return get_framework_module(framework).add_tool(tool, agent_name, path) |
| 92 | + |
| 93 | +def remove_tool(framework: str, tool: ToolConfig, agent_name: str, path: Optional[Path] = None): |
| 94 | + """ |
| 95 | + Remove a tool from the user's project. |
| 96 | + """ |
| 97 | + return get_framework_module(framework).remove_tool(tool, agent_name, path) |
| 98 | + |
| 99 | +def get_agent_names(framework: str, path: Optional[Path] = None) -> list[str]: |
| 100 | + """ |
| 101 | + Get a list of agent names in the user's project. |
| 102 | + """ |
| 103 | + return get_framework_module(framework).get_agent_names(path) |
| 104 | + |
| 105 | +def add_agent(framework: str, agent: AgentConfig, path: Optional[Path] = None): |
| 106 | + """ |
| 107 | + Add an agent to the user's project. |
| 108 | + """ |
| 109 | + return get_framework_module(framework).add_agent(agent, path) |
| 110 | + |
| 111 | +def add_task(framework: str, task: TaskConfig, path: Optional[Path] = None): |
| 112 | + """ |
| 113 | + Add a task to the user's project. |
| 114 | + """ |
| 115 | + return get_framework_module(framework).add_task(task, path) |
| 116 | + |
0 commit comments