Skip to content

Commit

Permalink
feat: Implement a goose run command
Browse files Browse the repository at this point in the history
  • Loading branch information
baxen committed Oct 6, 2024
1 parent f3aab12 commit f0fe586
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/goose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ def session_resume(name: Optional[str], profile: str, log_level: str) -> None:
session.run()


@goose_cli.command(name="run")
@click.argument("message_file", required=False, type=click.Path(exists=True))
@click.option("--profile")
@click.option("--log-level", type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), default="INFO")
def run(message_file: Optional[str], profile: str, log_level: str) -> None:
"""Run a single-pass session with a message from a markdown input file"""
if message_file:
with open(message_file, "r") as f:
initial_message = f.read()
else:
initial_message = click.get_text_stream("stdin").read()

session = Session(profile=profile, log_level=log_level)
session.single_pass(initial_message=initial_message)


@session.command(name="list")
def session_list() -> None:
"""List goose sessions"""
Expand Down
25 changes: 25 additions & 0 deletions src/goose/cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ def process_first_message(self) -> Optional[Message]:
return Message.user(text=user_input.text)
return self.exchange.messages.pop()

def single_pass(self, initial_message: str) -> None:
"""
Handles a single input message and processes a reply
without entering a loop for additional inputs.
Args:
initial_message (str): The initial user message to process.
"""
profile = self.profile_name or "default"
print(f"[dim]starting session | name:[cyan]{self.name}[/] profile:[cyan]{profile}[/]")
print(f"[dim]saving to {self.session_file_path}")
print()

# Process initial message
message = Message.user(initial_message)

self.exchange.add(message)
self.reply() # Process the user message

save_latest_session(self.session_file_path, self.exchange.messages)
print() # Print a newline for separation.

print(f"[dim]ended run | name:[cyan]{self.name}[/] profile:[cyan]{profile}[/]")
print("[dim]to resume: [magenta]goose session resume {self.name} --profile {profile}[/][/]")

def run(self) -> None:
"""
Runs the main loop to handle user inputs and responses.
Expand Down

0 comments on commit f0fe586

Please sign in to comment.