forked from agentstack-ai/AgentStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
224 lines (178 loc) · 6.52 KB
/
__init__.py
File metadata and controls
224 lines (178 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from typing import TYPE_CHECKING, Optional, Protocol, Callable
from types import ModuleType
from importlib import import_module
from pathlib import Path
from agentstack import conf
from agentstack.exceptions import ValidationError
from agentstack.utils import get_framework
from agentstack.agents import AgentConfig, get_all_agent_names
from agentstack.tasks import TaskConfig, get_all_task_names
from agentstack._tools import ToolConfig
from agentstack import graph
if TYPE_CHECKING:
from agentstack.generation import InsertionPoint
CREWAI = 'crewai'
LANGGRAPH = 'langgraph'
SUPPORTED_FRAMEWORKS = [
CREWAI,
LANGGRAPH,
]
class FrameworkModule(Protocol):
"""
Protocol spec for a framework implementation module.
"""
ENTRYPOINT: Path
"""
Relative path to the entrypoint file for the framework in the user's project.
ie. `src/crewai.py`
"""
def validate_project(self) -> None:
"""
Validate that a user's project is ready to run.
Raises a `ValidationError` if the project is not valid.
"""
...
def parse_llm(self, llm: str) -> tuple[str, str]:
"""
Parse a language model string into a provider and model.
"""
...
def add_tool(self, tool: ToolConfig, agent_name: str) -> None:
"""
Add a tool to an agent in the user's project.
"""
...
def remove_tool(self, tool: ToolConfig, agent_name: str) -> None:
"""
Remove a tool from an agent in user's project.
"""
...
def get_tool_callables(self, tool_name: str) -> list[Callable]:
"""
Get a tool by name and return it as a list of framework-native callables.
"""
...
def get_agent_method_names(self) -> list[str]:
"""
Get a list of agent names in the user's project.
"""
...
def get_agent_tool_names(self, agent_name: str) -> list[str]:
"""
Get a list of tool names in an agent in the user's project.
"""
...
def add_agent(self, agent: 'AgentConfig', position: Optional['InsertionPoint'] = None) -> None:
"""
Add an agent to the user's project.
"""
...
def add_task(self, task: 'TaskConfig', position: Optional['InsertionPoint'] = None) -> None:
"""
Add a task to the user's project.
"""
...
def get_task_method_names(self) -> list[str]:
"""
Get a list of task names in the user's project.
"""
...
def get_graph(self) -> list[graph.Edge]:
"""
Get the graph of the user's project.
"""
...
def get_framework_module(framework: str) -> FrameworkModule:
"""
Get the module for a framework.
"""
try:
return import_module(f".{framework}", package=__package__)
except ImportError:
raise Exception(f"Framework {framework} could not be imported.")
def get_entrypoint_path(framework: str) -> Path:
"""
Get the path to the entrypoint file for a framework.
"""
return conf.PATH / get_framework_module(framework).ENTRYPOINT
def validate_project():
"""
Validate that the user's project is ready to run.
"""
framework = get_framework()
entrypoint_path = get_entrypoint_path(framework)
_module = get_framework_module(framework)
# Run framework-specific validation
_module.validate_project()
# Verify that agents defined in agents.yaml are present in the codebase
agent_method_names = _module.get_agent_method_names()
for agent_name in get_all_agent_names():
if agent_name not in agent_method_names:
raise ValidationError(
f"Agent `{agent_name}` is defined in agents.yaml but not in {entrypoint_path}"
)
# Verify that tasks defined in tasks.yaml are present in the codebase
task_method_names = _module.get_task_method_names()
for task_name in get_all_task_names():
if task_name not in task_method_names:
raise ValidationError(
f"Task `{task_name}` is defined in tasks.yaml but not in {entrypoint_path}"
)
def parse_llm(llm: str) -> tuple[str, str]:
"""
Parse a language model string into a provider and model.
"""
return get_framework_module(get_framework()).parse_llm(llm)
def add_tool(tool: ToolConfig, agent_name: str):
"""
Add a tool to the user's project.
The tool will have already been installed in the user's application and have
all dependencies installed. We're just handling code generation here.
"""
return get_framework_module(get_framework()).add_tool(tool, agent_name)
def remove_tool(tool: ToolConfig, agent_name: str):
"""
Remove a tool from the user's project.
"""
return get_framework_module(get_framework()).remove_tool(tool, agent_name)
def get_tool_callables(tool_name: str) -> list[Callable]:
"""
Get a tool by name and return it as a list of framework-native callables.
"""
return get_framework_module(get_framework()).get_tool_callables(tool_name)
def get_agent_method_names() -> list[str]:
"""
Get a list of agent names in the user's project.
"""
return get_framework_module(get_framework()).get_agent_method_names()
def get_agent_tool_names(agent_name: str) -> list[str]:
"""
Get a list of tool names in the user's project.
"""
return get_framework_module(get_framework()).get_agent_tool_names(agent_name)
def add_agent(agent: 'AgentConfig', position: Optional['InsertionPoint'] = None):
"""
Add an agent to the user's project.
"""
framework = get_framework()
if agent.name in get_agent_method_names():
raise ValidationError(f"Agent `{agent.name}` already exists in {get_entrypoint_path(framework)}")
return get_framework_module(framework).add_agent(agent, position)
def add_task(task: 'TaskConfig', position: Optional['InsertionPoint'] = None):
"""
Add a task to the user's project.
"""
framework = get_framework()
if task.name in get_task_method_names():
raise ValidationError(f"Task `{task.name}` already exists in {get_entrypoint_path(framework)}")
return get_framework_module(framework).add_task(task, position)
def get_task_method_names() -> list[str]:
"""
Get a list of task names in the user's project.
"""
return get_framework_module(get_framework()).get_task_method_names()
def get_graph() -> list[graph.Edge]:
"""
Get the graph of the user's project.
"""
return get_framework_module(get_framework()).get_graph()