Skip to content
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

feat: support markdown plans #79

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/goose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from goose.cli.config import SESSIONS_PATH
from goose.cli.session import Session
from goose.toolkit.utils import render_template, parse_plan
from goose.utils import load_plugins
from goose.utils.session_file import list_sorted_session_files

Expand Down Expand Up @@ -73,11 +74,31 @@ def session_start(profile: str, plan: Optional[str] = None) -> None:
_plan = yaml.load(f)
else:
_plan = None

session = Session(profile=profile, plan=_plan)
session.run()


def parse_args(ctx: click.Context, param: click.Parameter, value: str) -> dict[str, str]:
if not value:
return {}
args = {}
for item in value.split(","):
key, val = item.split(":")
args[key.strip()] = val.strip()

return args


@session.command(name="planned")
@click.option("--plan", type=click.Path(exists=True))
@click.option("-a", "--args", callback=parse_args, help="Args in the format arg1:value1,arg2:value2")
def session_planned(plan: str, args: Optional[dict[str, str]]) -> None:
plan_templated = render_template(Path(plan), context=args)
_plan = parse_plan(plan_templated)
session = Session(plan=_plan)
session.run()


@session.command(name="resume")
@click.argument("name", required=False)
@click.option("--profile")
Expand Down
37 changes: 36 additions & 1 deletion src/goose/toolkit/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Optional
from typing import Optional, Dict

from pygments.lexers import get_lexer_for_filename
from pygments.util import ClassNotFound
Expand Down Expand Up @@ -42,3 +42,38 @@ def render_template(template_path: Path, context: Optional[dict] = None) -> str:
env = Environment(loader=FileSystemLoader(template_path.parent))
template = env.get_template(template_path.name)
return template.render(context or {})


def find_last_task_group_index(input_str: str) -> int:
lines = input_str.splitlines()
last_group_start_index = -1
current_group_start_index = -1

for i, line in enumerate(lines):
line = line.strip()
if line.startswith("-"):
# If this is the first line of a new group, mark its start
if current_group_start_index == -1:
current_group_start_index = i
else:
# If we encounter a non-hyphenated line and had a group, update last group start
if current_group_start_index != -1:
last_group_start_index = current_group_start_index
current_group_start_index = -1 # Reset for potential future groups

# If the input ended in a task group, update the last group index
if current_group_start_index != -1:
last_group_start_index = current_group_start_index
return last_group_start_index


def parse_plan(input_plan_str: str) -> Dict:
last_group_start_index = find_last_task_group_index(input_plan_str)
if last_group_start_index == -1:
return {"kickoff_message": input_plan_str, "tasks": []}

kickoff_message_list = input_plan_str.splitlines()[:last_group_start_index]
kickoff_message = "\n".join(kickoff_message_list).strip()
tasks_list = input_plan_str.splitlines()[last_group_start_index:]
tasks_list_output = [s[1:] for s in tasks_list if s.strip()] # filter leading -
return {"kickoff_message": kickoff_message, "tasks": tasks_list_output}
65 changes: 65 additions & 0 deletions tests/toolkit/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from goose.toolkit.utils import parse_plan


def test_parse_plan_simple():
plan_str = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious: is this our recommended style for multiline strings? what are the trade offs for this over triple-quoted multiline strings or even keeping the markdown as a fixture?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be honest, I did it to pass the ruff checks. Not sure if we've discussed it before

"Here is python repo\n"
"-use uv\n"
"-do not use poetry\n\n"
"Now you should:\n\n"
"-Open a file\n"
"-Run a test"
)
expected_result = {
"kickoff_message": "Here is python repo\n-use uv\n-do not use poetry\n\nNow you should:",
"tasks": ["Open a file", "Run a test"],
}
assert expected_result == parse_plan(plan_str)


def test_parse_plan_multiple_groups():
plan_str = (
"Here is python repo\n"
"-use uv\n"
"-do not use poetry\n\n"
"Now you should:\n\n"
"-Open a file\n"
"-Run a test\n\n"
"Now actually follow the steps:\n"
"-Step1\n"
"-Step2"
)
expected_result = {
"kickoff_message": (
"Here is python repo\n"
"-use uv\n"
"-do not use poetry\n\n"
"Now you should:\n\n"
"-Open a file\n"
"-Run a test\n\n"
"Now actually follow the steps:"
),
"tasks": ["Step1", "Step2"],
}
assert expected_result == parse_plan(plan_str)


def test_parse_plan_empty_tasks():
plan_str = "Here is python repo"
expected_result = {"kickoff_message": "Here is python repo", "tasks": []}
assert expected_result == parse_plan(plan_str)


def test_parse_plan_empty_kickoff_message():
plan_str = "-task1\n-task2"
expected_result = {"kickoff_message": "", "tasks": ["task1", "task2"]}
assert expected_result == parse_plan(plan_str)


def test_parse_plan_with_numbers():
plan_str = "Here is python repo\n" "Now you should:\n\n" "-1 Open a file\n" "-2 Run a test"
expected_result = {
"kickoff_message": "Here is python repo\nNow you should:",
"tasks": ["1 Open a file", "2 Run a test"],
}
assert expected_result == parse_plan(plan_str)