mini
Overview
mini
is a REPL-style interactive command line interface for using mini-SWE-agent in the local requirement (as opposed for workflows that require sandboxing or large scale batch processing).- Compared to
mini -v
,mini
is more lightweight and does not require threading.
Feedback wanted!
Give feedback on the mini
and mini -v
interfaces at this github issue
or in our Slack channel.
Command line options
Useful switches:
-h
/--help
: Show help-t
/--task
: Specify a task to run (else you will be prompted)-c
/--config
: Specify a config file to use, else we will usemini.yaml
or the configMSWEA_MINI_CONFIG_PATH
environment variable (see global configuration). It's enough to specify the name of the config file, e.g.,-c mini.yaml
(see global configuration for how it is resolved).-m
/--model
: Specify a model to use, else we will use the modelMSWEA_MODEL_NAME
environment variable (see global configuration)-y
/--yolo
: Start inyolo
mode (see below)
Modes of operation
mini
provides three different modes of operation
confirm
(/c
): The LM proposes an action and the user is prompted to confirm (press Enter) or reject (enter a rejection message)yolo
(/y
): The action from the LM is executed immediately without confirmationhuman
(/u
): The user takes over to type and execute commands
You can switch between the modes with the /c
, /y
, and /u
commands that you can enter any time the agent is waiting for input.
You can also press Ctrl+C
to interrupt the agent at any time, allowing you to switch between modes.
mini
starts in confirm
mode by default. To start in yolo
mode, you can add -y
/--yolo
to the command line.
Miscellaneous tips
mini
saves the full history of your last run to your global config directory. The path to the directory is printed when you startmini
.
Implementation
Default config
agent:
system_template: |
You are a helpful assistant that can interact with a computer.
Your response must contain exactly ONE bash code block with ONE command (or commands connected with && or ||).
Include a THOUGHT section before your command where you explain your reasoning process.
Format your response as shown in <format_example>.
<format_example>
Your reasoning and analysis here. Explain why you want to perform the action.
```bash
your_command_here
```
</format_example>
Failure to follow these rules will cause your response to be rejected.
instance_template: |
Please solve this issue: {{task}}
You can execute bash commands and edit files to implement the necessary changes.
## Recommended Workflow
This workflows should be done step-by-step so that you can iterate on your changes and any possible problems.
1. Analyze the codebase by finding and reading relevant files
2. Create a script to reproduce the issue
3. Edit the source code to resolve the issue
4. Verify your fix works by running your script again
5. Test edge cases to ensure your fix is robust
6. Submit your changes and finish your work by issuing the following command: `echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT`.
Do not combine it with any other command. <important>After this command, you cannot continue working on this task.</important>
## Important Rules
1. Every response must contain exactly one action
2. The action must be enclosed in triple backticks
3. Directory or environment variable changes are not persistent. Every action is executed in a new subshell.
However, you can prefix any action with `MY_ENV_VAR=MY_VALUE cd /path/to/working/dir && ...` or write/load environment variables from files
<system_information>
{{system}} {{release}} {{version}} {{machine}} {{processor}}
</system_information>
## Formatting your response
Here is an example of a correct response:
<example_response>
THOUGHT: I need to understand the structure of the repository first. Let me check what files are in the current directory to get a better understanding of the codebase.
```bash
ls -la
```
</example_response>
## Useful command examples
### Create a new file:
```bash
cat <<'EOF' > newfile.py
import numpy as np
hello = "world"
print(hello)
EOF
```
### Edit files with sed:
{%- if system == "Darwin" -%}
<important>
You are on MacOS. For all the below examples, you need to use `sed -i ''` instead of `sed -i`.
</important>
{%- endif -%}
```bash
# Replace all occurrences
sed -i 's/old_string/new_string/g' filename.py
# Replace only first occurrence
sed -i 's/old_string/new_string/' filename.py
# Replace first occurrence on line 1
sed -i '1s/old_string/new_string/' filename.py
# Replace all occurrences in lines 1-10
sed -i '1,10s/old_string/new_string/g' filename.py
```
### View file content:
```bash
# View specific lines with numbers
nl -ba filename.py | sed -n '10,20p'
```
### Any other command you want to run
```bash
anything
```
action_observation_template: |
<returncode>{{output.returncode}}</returncode>
{% if output.output | length < 10000 -%}
<output>
{{ output.output -}}
</output>
{%- else -%}
<warning>
The output of your last command was too long.
Please try a different command that produces less output.
If you're looking at a file you can try use head, tail or sed to view a smaller number of lines selectively.
If you're using grep or find and it produced too much output, you can use a more selective search pattern.
If you really need to see something from the full command's output, you can redirect output to a file and then search in that file.
</warning>
{%- set elided_chars = output.output | length - 10000 -%}
<output_head>
{{ output.output[:5000] }}
</output_head>
<elided_chars>
{{ elided_chars }} characters elided
</elided_chars>
<output_tail>
{{ output.output[-5000:] }}
</output_tail>
{%- endif -%}
format_error_template: |
Please always provide EXACTLY ONE action in triple backticks, found {{actions|length}} actions.
If you want to end the task, please issue the following command: `echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT`
without any other command.
Else, please format your response exactly as follows:
<response_example>
Here are some thoughts about why you want to perform the action.
```bash
<action>
```
</response_example>
Note: In rare cases, if you need to reference a similar format in your command, you might have
to proceed in two steps, first writing TRIPLEBACKTICKSBASH, then replacing them with ```bash.
step_limit: 0.
cost_limit: 3.
mode: confirm
environment:
env:
PAGER: cat
MANPAGER: cat
LESS: -R
PIP_PROGRESS_BAR: 'off'
TQDM_DISABLE: '1'
model:
model_kwargs:
temperature: 0.0
drop_params: true
Run script
#!/usr/bin/env python3
"""Run mini-SWE-agent in your local environment. This is the default executable `mini`."""
# Read this first: https://mini-swe-agent.com/latest/usage/mini/ (usage)
import os
import traceback
from pathlib import Path
from typing import Any
import typer
import yaml
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.history import FileHistory
from prompt_toolkit.shortcuts import PromptSession
from rich.console import Console
from minisweagent import global_config_dir
from minisweagent.agents.interactive import InteractiveAgent
from minisweagent.agents.interactive_textual import TextualAgent
from minisweagent.config import builtin_config_dir, get_config_path
from minisweagent.environments.local import LocalEnvironment
from minisweagent.models import get_model
from minisweagent.run.extra.config import configure_if_first_time
from minisweagent.run.utils.save import save_traj
from minisweagent.utils.log import logger
DEFAULT_CONFIG = Path(os.getenv("MSWEA_MINI_CONFIG_PATH", builtin_config_dir / "mini.yaml"))
DEFAULT_OUTPUT = global_config_dir / "last_mini_run.traj.json"
console = Console(highlight=False)
app = typer.Typer(rich_markup_mode="rich")
prompt_session = PromptSession(history=FileHistory(global_config_dir / "mini_task_history.txt"))
_HELP_TEXT = """Run mini-SWE-agent in your local environment.
[not dim]
There are two different user interfaces:
[bold green]mini[/bold green] Simple REPL-style interface
[bold green]mini -v[/bold green] Pager-style interface (Textual)
More information about the usage: [bold green]https://mini-swe-agent.com/latest/usage/mini/[/bold green]
[/not dim]
"""
# fmt: off
@app.command(help=_HELP_TEXT)
def main(
visual: bool = typer.Option(False, "-v", "--visual", help="Toggle (pager-style) UI (Textual) depending on the MSWEA_VISUAL_MODE_DEFAULT environment setting",),
model_name: str | None = typer.Option( None, "-m", "--model", help="Model to use",),
model_class: str | None = typer.Option(None, "--model-class", help="Model class to use (e.g., 'anthropic' or 'minisweagent.models.anthropic.AnthropicModel')", rich_help_panel="Advanced"),
task: str | None = typer.Option(None, "-t", "--task", help="Task/problem statement", show_default=False),
yolo: bool = typer.Option(False, "-y", "--yolo", help="Run without confirmation"),
cost_limit: float | None = typer.Option(None, "-l", "--cost-limit", help="Cost limit. Set to 0 to disable."),
config_spec: Path = typer.Option(DEFAULT_CONFIG, "-c", "--config", help="Path to config file"),
output: Path | None = typer.Option(DEFAULT_OUTPUT, "-o", "--output", help="Output trajectory file"),
exit_immediately: bool = typer.Option( False, "--exit-immediately", help="Exit immediately when the agent wants to finish instead of prompting.", rich_help_panel="Advanced"),
) -> Any:
# fmt: on
configure_if_first_time()
config = yaml.safe_load(get_config_path(config_spec).read_text())
if not task:
console.print("[bold yellow]What do you want to do?")
task = prompt_session.prompt(
"",
multiline=True,
bottom_toolbar=HTML(
"Submit task: <b fg='yellow' bg='black'>Esc+Enter</b> | "
"Navigate history: <b fg='yellow' bg='black'>Arrow Up/Down</b> | "
"Search history: <b fg='yellow' bg='black'>Ctrl+R</b>"
),
)
console.print("[bold green]Got that, thanks![/bold green]")
if yolo:
config.setdefault("agent", {})["mode"] = "yolo"
if cost_limit:
config.setdefault("agent", {})["cost_limit"] = cost_limit
if exit_immediately:
config.setdefault("agent", {})["confirm_exit"] = False
if model_class is not None:
config.setdefault("model", {})["model_class"] = model_class
model = get_model(model_name, config.get("model", {}))
env = LocalEnvironment(**config.get("env", {}))
# Both visual flag and the MSWEA_VISUAL_MODE_DEFAULT flip the mode, so it's essentially a XOR
agent_class = InteractiveAgent
if visual == (os.getenv("MSWEA_VISUAL_MODE_DEFAULT", "false") == "false"):
agent_class = TextualAgent
agent = agent_class(model, env, **config.get("agent", {}))
exit_status, result, extra_info = None, None, None
try:
exit_status, result = agent.run(task) # type: ignore[arg-type]
except Exception as e:
logger.error(f"Error running agent: {e}", exc_info=True)
exit_status, result = type(e).__name__, str(e)
extra_info = {"traceback": traceback.format_exc()}
finally:
if output:
save_traj(agent, output, exit_status=exit_status, result=result, extra_info=extra_info) # type: ignore[arg-type]
return agent
if __name__ == "__main__":
app()
Agent class
"""A small generalization of the default agent that puts the user in the loop.
There are three modes:
- human: commands issued by the user are executed immediately
- confirm: commands issued by the LM but not whitelisted are confirmed by the user
- yolo: commands issued by the LM are executed immediately without confirmation
"""
import re
from dataclasses import dataclass, field
from typing import Literal
from prompt_toolkit.history import FileHistory
from prompt_toolkit.shortcuts import PromptSession
from rich.console import Console
from rich.rule import Rule
from minisweagent import global_config_dir
from minisweagent.agents.default import AgentConfig, DefaultAgent, LimitsExceeded, NonTerminatingException, Submitted
console = Console(highlight=False)
prompt_session = PromptSession(history=FileHistory(global_config_dir / "interactive_history.txt"))
@dataclass
class InteractiveAgentConfig(AgentConfig):
mode: Literal["human", "confirm", "yolo"] = "confirm"
"""Whether to confirm actions."""
whitelist_actions: list[str] = field(default_factory=list)
"""Never confirm actions that match these regular expressions."""
confirm_exit: bool = True
"""If the agent wants to finish, do we ask for confirmation from user?"""
class InteractiveAgent(DefaultAgent):
_MODE_COMMANDS_MAPPING = {"/u": "human", "/c": "confirm", "/y": "yolo"}
def __init__(self, *args, config_class=InteractiveAgentConfig, **kwargs):
super().__init__(*args, config_class=config_class, **kwargs)
self.cost_last_confirmed = 0.0
def add_message(self, role: str, content: str, **kwargs):
# Extend supermethod to print messages
super().add_message(role, content, **kwargs)
if role == "assistant":
console.print(
f"\n[red][bold]mini-swe-agent[/bold] (step [bold]{self.model.n_calls}[/bold], [bold]${self.model.cost:.2f}[/bold]):[/red]\n",
end="",
highlight=False,
)
else:
console.print(f"\n[bold green]{role.capitalize()}[/bold green]:\n", end="", highlight=False)
console.print(content, highlight=False, markup=False)
def query(self) -> dict:
# Extend supermethod to handle human mode
if self.config.mode == "human":
match command := self._prompt_and_handle_special("[bold yellow]>[/bold yellow] "):
case "/y" | "/c": # Just go to the super query, which queries the LM for the next action
pass
case _:
msg = {"content": f"\n```bash\n{command}\n```"}
self.add_message("assistant", msg["content"])
return msg
try:
with console.status("Waiting for the LM to respond..."):
return super().query()
except LimitsExceeded:
console.print(
f"Limits exceeded. Limits: {self.config.step_limit} steps, ${self.config.cost_limit}.\n"
f"Current spend: {self.model.n_calls} steps, ${self.model.cost:.2f}."
)
self.config.step_limit = int(input("New step limit: "))
self.config.cost_limit = float(input("New cost limit: "))
return super().query()
def step(self) -> dict:
# Override the step method to handle user interruption
try:
console.print(Rule())
return super().step()
except KeyboardInterrupt:
# We always add a message about the interrupt and then just proceed to the next step
interruption_message = self._prompt_and_handle_special(
"\n\n[bold yellow]Interrupted.[/bold yellow] "
"[green]Type a comment/command[/green] (/h for available commands)"
"\n[bold yellow]>[/bold yellow] "
).strip()
if not interruption_message or interruption_message in self._MODE_COMMANDS_MAPPING:
interruption_message = "Temporary interruption caught."
raise NonTerminatingException(f"Interrupted by user: {interruption_message}")
def execute_action(self, action: dict) -> dict:
# Override the execute_action method to handle user confirmation
if self.should_ask_confirmation(action["action"]):
self.ask_confirmation()
return super().execute_action(action)
def should_ask_confirmation(self, action: str) -> bool:
return self.config.mode == "confirm" and not any(re.match(r, action) for r in self.config.whitelist_actions)
def ask_confirmation(self) -> None:
prompt = (
"[bold yellow]Execute?[/bold yellow] [green][bold]Enter[/bold] to confirm[/green], "
"or [green]Type a comment/command[/green] (/h for available commands)\n"
"[bold yellow]>[/bold yellow] "
)
match user_input := self._prompt_and_handle_special(prompt).strip():
case "" | "/y":
pass # confirmed, do nothing
case "/u": # Skip execution action and get back to query
raise NonTerminatingException("Command not executed. Switching to human mode")
case _:
raise NonTerminatingException(
f"Command not executed. The user rejected your command with the following message: {user_input}"
)
def _prompt_and_handle_special(self, prompt: str) -> str:
"""Prompts the user, takes care of /h (followed by requery) and sets the mode. Returns the user input."""
console.print(prompt, end="")
user_input = prompt_session.prompt("")
if user_input == "/h":
console.print(
f"Current mode: [bold green]{self.config.mode}[/bold green]\n"
f"[bold green]/y[/bold green] to switch to [bold yellow]yolo[/bold yellow] mode (execute LM commands without confirmation)\n"
f"[bold green]/c[/bold green] to switch to [bold yellow]confirmation[/bold yellow] mode (ask for confirmation before executing LM commands)\n"
f"[bold green]/u[/bold green] to switch to [bold yellow]human[/bold yellow] mode (execute commands issued by the user)\n"
)
return self._prompt_and_handle_special(prompt)
if user_input in self._MODE_COMMANDS_MAPPING:
if self.config.mode == self._MODE_COMMANDS_MAPPING[user_input]:
return self._prompt_and_handle_special(
f"[bold red]Already in {self.config.mode} mode.[/bold red]\n{prompt}"
)
self.config.mode = self._MODE_COMMANDS_MAPPING[user_input]
console.print(f"Switched to [bold green]{self.config.mode}[/bold green] mode.")
return user_input
return user_input
def has_finished(self, output: dict[str, str]):
try:
return super().has_finished(output)
except Submitted as e:
if self.config.confirm_exit:
console.print(
"[bold green]Agent wants to finish.[/bold green] "
"[green]Type a comment to give it a new task or press enter to quit.\n"
"[bold yellow]>[/bold yellow] ",
end="",
)
if new_task := self._prompt_and_handle_special("").strip():
raise NonTerminatingException(f"The user added a new task: {new_task}")
raise e