Skip to content

Commit

Permalink
working on code insert
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Nov 14, 2024
1 parent b0f1cda commit 1376d76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 29 additions & 0 deletions agentstack/generation/gen_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import ast


def insert_code_after_tag(file_path, tag, code_to_insert, next_line=False):
if next_line:
code_to_insert = ['\n'] + code_to_insert
Expand All @@ -16,3 +19,29 @@ def insert_code_after_tag(file_path, tag, code_to_insert, next_line=False):

with open(file_path, 'w') as file:
file.writelines(lines)


def insert_after_tasks(file_path, code_to_insert):
with open(file_path, 'r') as file:
content = file.read()

module = ast.parse(content)

# Track the last task function's end line
last_task_end = None
for node in ast.walk(module):
if isinstance(node, ast.FunctionDef) and \
any(isinstance(deco, ast.Name) and deco.id == 'task' for deco in node.decorator_list):
last_task_end = node.end_lineno

if last_task_end is not None:
lines = content.split('\n')
for i, line in enumerate(code_to_insert):
lines.insert(last_task_end + i, line)
content = '\n'.join(lines)

with open(file_path, 'w') as file:
file.write(content)
return True
return False

5 changes: 2 additions & 3 deletions agentstack/generation/task_generation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from .gen_utils import insert_code_after_tag
from .gen_utils import insert_code_after_tag, insert_after_tasks
from ..utils import verify_agentstack_project, get_framework
import os
from ruamel.yaml import YAML
Expand Down Expand Up @@ -77,7 +77,6 @@ def generate_crew_task(

# Add task to crew.py
file_path = 'src/crew.py'
tag = '# Task definitions'
code_to_insert = [
"@task",
f"def {name}(self) -> Task:",
Expand All @@ -87,4 +86,4 @@ def generate_crew_task(
""
]

insert_code_after_tag(file_path, tag, code_to_insert)
insert_after_tasks(file_path, code_to_insert)

0 comments on commit 1376d76

Please sign in to comment.