mini-swe-agent 1.16.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. mini_swe_agent-1.16.0.dist-info/METADATA +314 -0
  2. mini_swe_agent-1.16.0.dist-info/RECORD +62 -0
  3. mini_swe_agent-1.16.0.dist-info/WHEEL +5 -0
  4. mini_swe_agent-1.16.0.dist-info/entry_points.txt +5 -0
  5. mini_swe_agent-1.16.0.dist-info/licenses/LICENSE.md +21 -0
  6. mini_swe_agent-1.16.0.dist-info/top_level.txt +1 -0
  7. minisweagent/__init__.py +83 -0
  8. minisweagent/__main__.py +7 -0
  9. minisweagent/agents/__init__.py +1 -0
  10. minisweagent/agents/default.py +131 -0
  11. minisweagent/agents/interactive.py +153 -0
  12. minisweagent/agents/interactive_textual.py +450 -0
  13. minisweagent/config/README.md +10 -0
  14. minisweagent/config/__init__.py +27 -0
  15. minisweagent/config/default.yaml +157 -0
  16. minisweagent/config/extra/__init__.py +1 -0
  17. minisweagent/config/extra/swebench.yaml +230 -0
  18. minisweagent/config/extra/swebench_roulette.yaml +233 -0
  19. minisweagent/config/extra/swebench_xml.yaml +215 -0
  20. minisweagent/config/github_issue.yaml +146 -0
  21. minisweagent/config/mini.tcss +86 -0
  22. minisweagent/config/mini.yaml +158 -0
  23. minisweagent/config/mini_no_temp.yaml +158 -0
  24. minisweagent/environments/__init__.py +31 -0
  25. minisweagent/environments/docker.py +114 -0
  26. minisweagent/environments/extra/__init__.py +0 -0
  27. minisweagent/environments/extra/bubblewrap.py +112 -0
  28. minisweagent/environments/extra/swerex_docker.py +47 -0
  29. minisweagent/environments/local.py +38 -0
  30. minisweagent/environments/singularity.py +97 -0
  31. minisweagent/models/__init__.py +114 -0
  32. minisweagent/models/anthropic.py +35 -0
  33. minisweagent/models/extra/__init__.py +0 -0
  34. minisweagent/models/extra/roulette.py +61 -0
  35. minisweagent/models/litellm_model.py +100 -0
  36. minisweagent/models/litellm_response_api_model.py +80 -0
  37. minisweagent/models/openrouter_model.py +125 -0
  38. minisweagent/models/portkey_model.py +154 -0
  39. minisweagent/models/portkey_response_api_model.py +74 -0
  40. minisweagent/models/requesty_model.py +119 -0
  41. minisweagent/models/test_models.py +42 -0
  42. minisweagent/models/utils/__init__.py +0 -0
  43. minisweagent/models/utils/cache_control.py +54 -0
  44. minisweagent/models/utils/key_per_thread.py +20 -0
  45. minisweagent/models/utils/openai_utils.py +41 -0
  46. minisweagent/py.typed +0 -0
  47. minisweagent/run/__init__.py +1 -0
  48. minisweagent/run/extra/__init__.py +0 -0
  49. minisweagent/run/extra/config.py +114 -0
  50. minisweagent/run/extra/swebench.py +266 -0
  51. minisweagent/run/extra/swebench_single.py +79 -0
  52. minisweagent/run/extra/utils/__init__.py +0 -0
  53. minisweagent/run/extra/utils/batch_progress.py +178 -0
  54. minisweagent/run/github_issue.py +87 -0
  55. minisweagent/run/hello_world.py +36 -0
  56. minisweagent/run/inspector.py +212 -0
  57. minisweagent/run/mini.py +108 -0
  58. minisweagent/run/mini_extra.py +44 -0
  59. minisweagent/run/utils/__init__.py +0 -0
  60. minisweagent/run/utils/save.py +78 -0
  61. minisweagent/utils/__init__.py +0 -0
  62. minisweagent/utils/log.py +36 -0
@@ -0,0 +1,153 @@
1
+ """A small generalization of the default agent that puts the user in the loop.
2
+
3
+ There are three modes:
4
+ - human: commands issued by the user are executed immediately
5
+ - confirm: commands issued by the LM but not whitelisted are confirmed by the user
6
+ - yolo: commands issued by the LM are executed immediately without confirmation
7
+ """
8
+
9
+ import re
10
+ from dataclasses import dataclass, field
11
+ from typing import Literal
12
+
13
+ from prompt_toolkit.history import FileHistory
14
+ from prompt_toolkit.shortcuts import PromptSession
15
+ from rich.console import Console
16
+ from rich.rule import Rule
17
+
18
+ from minisweagent import global_config_dir
19
+ from minisweagent.agents.default import AgentConfig, DefaultAgent, LimitsExceeded, NonTerminatingException, Submitted
20
+
21
+ console = Console(highlight=False)
22
+ prompt_session = PromptSession(history=FileHistory(global_config_dir / "interactive_history.txt"))
23
+
24
+
25
+ @dataclass
26
+ class InteractiveAgentConfig(AgentConfig):
27
+ mode: Literal["human", "confirm", "yolo"] = "confirm"
28
+ """Whether to confirm actions."""
29
+ whitelist_actions: list[str] = field(default_factory=list)
30
+ """Never confirm actions that match these regular expressions."""
31
+ confirm_exit: bool = True
32
+ """If the agent wants to finish, do we ask for confirmation from user?"""
33
+
34
+
35
+ class InteractiveAgent(DefaultAgent):
36
+ _MODE_COMMANDS_MAPPING = {"/u": "human", "/c": "confirm", "/y": "yolo"}
37
+
38
+ def __init__(self, *args, config_class=InteractiveAgentConfig, **kwargs):
39
+ super().__init__(*args, config_class=config_class, **kwargs)
40
+ self.cost_last_confirmed = 0.0
41
+
42
+ def add_message(self, role: str, content: str, **kwargs):
43
+ # Extend supermethod to print messages
44
+ super().add_message(role, content, **kwargs)
45
+ if role == "assistant":
46
+ console.print(
47
+ f"\n[red][bold]mini-swe-agent[/bold] (step [bold]{self.model.n_calls}[/bold], [bold]${self.model.cost:.2f}[/bold]):[/red]\n",
48
+ end="",
49
+ highlight=False,
50
+ )
51
+ else:
52
+ console.print(f"\n[bold green]{role.capitalize()}[/bold green]:\n", end="", highlight=False)
53
+ console.print(content, highlight=False, markup=False)
54
+
55
+ def query(self) -> dict:
56
+ # Extend supermethod to handle human mode
57
+ if self.config.mode == "human":
58
+ match command := self._prompt_and_handle_special("[bold yellow]>[/bold yellow] "):
59
+ case "/y" | "/c": # Just go to the super query, which queries the LM for the next action
60
+ pass
61
+ case _:
62
+ msg = {"content": f"\n```bash\n{command}\n```"}
63
+ self.add_message("assistant", msg["content"])
64
+ return msg
65
+ try:
66
+ with console.status("Waiting for the LM to respond..."):
67
+ return super().query()
68
+ except LimitsExceeded:
69
+ console.print(
70
+ f"Limits exceeded. Limits: {self.config.step_limit} steps, ${self.config.cost_limit}.\n"
71
+ f"Current spend: {self.model.n_calls} steps, ${self.model.cost:.2f}."
72
+ )
73
+ self.config.step_limit = int(input("New step limit: "))
74
+ self.config.cost_limit = float(input("New cost limit: "))
75
+ return super().query()
76
+
77
+ def step(self) -> dict:
78
+ # Override the step method to handle user interruption
79
+ try:
80
+ console.print(Rule())
81
+ return super().step()
82
+ except KeyboardInterrupt:
83
+ # We always add a message about the interrupt and then just proceed to the next step
84
+ interruption_message = self._prompt_and_handle_special(
85
+ "\n\n[bold yellow]Interrupted.[/bold yellow] "
86
+ "[green]Type a comment/command[/green] (/h for available commands)"
87
+ "\n[bold yellow]>[/bold yellow] "
88
+ ).strip()
89
+ if not interruption_message or interruption_message in self._MODE_COMMANDS_MAPPING:
90
+ interruption_message = "Temporary interruption caught."
91
+ raise NonTerminatingException(f"Interrupted by user: {interruption_message}")
92
+
93
+ def execute_action(self, action: dict) -> dict:
94
+ # Override the execute_action method to handle user confirmation
95
+ if self.should_ask_confirmation(action["action"]):
96
+ self.ask_confirmation()
97
+ return super().execute_action(action)
98
+
99
+ def should_ask_confirmation(self, action: str) -> bool:
100
+ return self.config.mode == "confirm" and not any(re.match(r, action) for r in self.config.whitelist_actions)
101
+
102
+ def ask_confirmation(self) -> None:
103
+ prompt = (
104
+ "[bold yellow]Execute?[/bold yellow] [green][bold]Enter[/bold] to confirm[/green], "
105
+ "or [green]Type a comment/command[/green] (/h for available commands)\n"
106
+ "[bold yellow]>[/bold yellow] "
107
+ )
108
+ match user_input := self._prompt_and_handle_special(prompt).strip():
109
+ case "" | "/y":
110
+ pass # confirmed, do nothing
111
+ case "/u": # Skip execution action and get back to query
112
+ raise NonTerminatingException("Command not executed. Switching to human mode")
113
+ case _:
114
+ raise NonTerminatingException(
115
+ f"Command not executed. The user rejected your command with the following message: {user_input}"
116
+ )
117
+
118
+ def _prompt_and_handle_special(self, prompt: str) -> str:
119
+ """Prompts the user, takes care of /h (followed by requery) and sets the mode. Returns the user input."""
120
+ console.print(prompt, end="")
121
+ user_input = prompt_session.prompt("")
122
+ if user_input == "/h":
123
+ console.print(
124
+ f"Current mode: [bold green]{self.config.mode}[/bold green]\n"
125
+ f"[bold green]/y[/bold green] to switch to [bold yellow]yolo[/bold yellow] mode (execute LM commands without confirmation)\n"
126
+ f"[bold green]/c[/bold green] to switch to [bold yellow]confirmation[/bold yellow] mode (ask for confirmation before executing LM commands)\n"
127
+ f"[bold green]/u[/bold green] to switch to [bold yellow]human[/bold yellow] mode (execute commands issued by the user)\n"
128
+ )
129
+ return self._prompt_and_handle_special(prompt)
130
+ if user_input in self._MODE_COMMANDS_MAPPING:
131
+ if self.config.mode == self._MODE_COMMANDS_MAPPING[user_input]:
132
+ return self._prompt_and_handle_special(
133
+ f"[bold red]Already in {self.config.mode} mode.[/bold red]\n{prompt}"
134
+ )
135
+ self.config.mode = self._MODE_COMMANDS_MAPPING[user_input]
136
+ console.print(f"Switched to [bold green]{self.config.mode}[/bold green] mode.")
137
+ return user_input
138
+ return user_input
139
+
140
+ def has_finished(self, output: dict[str, str]):
141
+ try:
142
+ return super().has_finished(output)
143
+ except Submitted as e:
144
+ if self.config.confirm_exit:
145
+ console.print(
146
+ "[bold green]Agent wants to finish.[/bold green] "
147
+ "[green]Type a comment to give it a new task or press enter to quit.\n"
148
+ "[bold yellow]>[/bold yellow] ",
149
+ end="",
150
+ )
151
+ if new_task := self._prompt_and_handle_special("").strip():
152
+ raise NonTerminatingException(f"The user added a new task: {new_task}")
153
+ raise e
@@ -0,0 +1,450 @@
1
+ """
2
+ Extension of the `default.py` agent that uses Textual for an interactive TUI.
3
+ For a simpler version of an interactive UI that does not require threading and more, see `interactive.py`.
4
+ """
5
+
6
+ import logging
7
+ import os
8
+ import re
9
+ import threading
10
+ import time
11
+ import traceback
12
+ from collections.abc import Iterable
13
+ from dataclasses import dataclass, field
14
+ from pathlib import Path
15
+ from typing import Literal
16
+
17
+ from rich.spinner import Spinner
18
+ from rich.text import Text
19
+ from textual.app import App, ComposeResult, SystemCommand
20
+ from textual.binding import Binding
21
+ from textual.containers import Container, Vertical, VerticalScroll
22
+ from textual.css.query import NoMatches
23
+ from textual.events import Key
24
+ from textual.screen import Screen
25
+ from textual.widgets import Footer, Header, Input, Static, TextArea
26
+
27
+ from minisweagent.agents.default import AgentConfig, DefaultAgent, NonTerminatingException, Submitted
28
+
29
+
30
+ @dataclass
31
+ class TextualAgentConfig(AgentConfig):
32
+ mode: Literal["confirm", "yolo"] = "confirm"
33
+ """Mode for action execution: 'confirm' requires user confirmation, 'yolo' executes immediately."""
34
+ whitelist_actions: list[str] = field(default_factory=list)
35
+ """Never confirm actions that match these regular expressions."""
36
+ confirm_exit: bool = True
37
+ """If the agent wants to finish, do we ask for confirmation from user?"""
38
+
39
+
40
+ class _TextualAgent(DefaultAgent):
41
+ def __init__(self, app: "TextualAgent", *args, **kwargs):
42
+ """Connects the DefaultAgent to the TextualApp."""
43
+ self.app = app
44
+ super().__init__(*args, config_class=TextualAgentConfig, **kwargs)
45
+ self._current_action_from_human = False
46
+
47
+ def add_message(self, role: str, content: str, **kwargs):
48
+ super().add_message(role, content, **kwargs)
49
+ if self.app.agent_state != "UNINITIALIZED":
50
+ self.app.call_from_thread(self.app.on_message_added)
51
+
52
+ def query(self) -> dict:
53
+ if self.config.mode == "human":
54
+ human_input = self.app.input_container.request_input("Enter your command:")
55
+ self._current_action_from_human = True
56
+ msg = {"content": f"\n```bash\n{human_input}\n```"}
57
+ self.add_message("assistant", msg["content"])
58
+ return msg
59
+ self._current_action_from_human = False
60
+ return super().query()
61
+
62
+ def run(self, task: str, **kwargs) -> tuple[str, str]:
63
+ try:
64
+ exit_status, result = super().run(task, **kwargs)
65
+ except Exception as e:
66
+ result = str(e)
67
+ self.app.call_from_thread(self.app.action_quit)
68
+ print(traceback.format_exc())
69
+ return "ERROR", result
70
+ else:
71
+ self.app.call_from_thread(self.app.on_agent_finished, exit_status, result)
72
+ self.app.call_from_thread(self.app.action_quit)
73
+ return exit_status, result
74
+
75
+ def execute_action(self, action: dict) -> dict:
76
+ if self.config.mode == "human" and not self._current_action_from_human: # threading, grrrrr
77
+ raise NonTerminatingException("Command not executed because user switched to manual mode.")
78
+ if (
79
+ self.config.mode == "confirm"
80
+ and action["action"].strip()
81
+ and not any(re.match(r, action["action"]) for r in self.config.whitelist_actions)
82
+ ):
83
+ result = self.app.input_container.request_input("Press ENTER to confirm or provide rejection reason")
84
+ if result: # Non-empty string means rejection
85
+ raise NonTerminatingException(f"Command not executed: {result}")
86
+ return super().execute_action(action)
87
+
88
+ def has_finished(self, output: dict[str, str]):
89
+ try:
90
+ return super().has_finished(output)
91
+ except Submitted as e:
92
+ if self.config.confirm_exit:
93
+ if new_task := self.app.input_container.request_input(
94
+ "[bold green]Agent wants to finish.[/bold green] "
95
+ "[green]Type a comment to give it a new task or press enter to quit.\n"
96
+ ).strip():
97
+ raise NonTerminatingException(f"The user added a new task: {new_task}")
98
+ raise e
99
+
100
+
101
+ class AddLogEmitCallback(logging.Handler):
102
+ def __init__(self, callback):
103
+ """Custom log handler that forwards messages via callback."""
104
+ super().__init__()
105
+ self.callback = callback
106
+
107
+ def emit(self, record: logging.LogRecord):
108
+ self.callback(record) # type: ignore[attr-defined]
109
+
110
+
111
+ def _messages_to_steps(messages: list[dict]) -> list[list[dict]]:
112
+ """Group messages into "pages" as shown by the UI."""
113
+ steps = []
114
+ current_step = []
115
+ for message in messages:
116
+ current_step.append(message)
117
+ if message["role"] == "user":
118
+ steps.append(current_step)
119
+ current_step = []
120
+ if current_step:
121
+ steps.append(current_step)
122
+ return steps
123
+
124
+
125
+ class SmartInputContainer(Container):
126
+ def __init__(self, app: "TextualAgent"):
127
+ """Smart input container supporting single-line and multi-line input modes."""
128
+ super().__init__(classes="smart-input-container")
129
+ self._app = app
130
+ self._multiline_mode = False
131
+ self.can_focus = True
132
+ self.display = False
133
+
134
+ self.pending_prompt: str | None = None
135
+ self._input_event = threading.Event()
136
+ self._input_result: str | None = None
137
+
138
+ self._header_display = Static(id="input-header-display", classes="message-header input-request-header")
139
+ self._hint_text = Static(classes="hint-text")
140
+ self._single_input = Input(placeholder="Type your input...")
141
+ self._multi_input = TextArea(show_line_numbers=False, classes="multi-input")
142
+ self._input_elements_container = Vertical(
143
+ self._header_display,
144
+ self._hint_text,
145
+ self._single_input,
146
+ self._multi_input,
147
+ classes="message-container",
148
+ )
149
+
150
+ def compose(self) -> ComposeResult:
151
+ yield self._input_elements_container
152
+
153
+ def on_mount(self) -> None:
154
+ """Initialize the widget state."""
155
+ self._multi_input.display = False
156
+ self._update_mode_display()
157
+
158
+ def on_focus(self) -> None:
159
+ """Called when the container gains focus."""
160
+ if self._multiline_mode:
161
+ self._multi_input.focus()
162
+ else:
163
+ self._single_input.focus()
164
+
165
+ def request_input(self, prompt: str) -> str:
166
+ """Request input from user. Returns input text (empty string if confirmed without reason)."""
167
+ self._input_event.clear()
168
+ self._input_result = None
169
+ self.pending_prompt = prompt
170
+ self._header_display.update(prompt)
171
+ self._update_mode_display()
172
+ self._app.call_from_thread(self._app.update_content)
173
+ self._input_event.wait()
174
+ return self._input_result or ""
175
+
176
+ def _complete_input(self, input_text: str):
177
+ """Internal method to complete the input process."""
178
+ self._input_result = input_text
179
+ self.pending_prompt = None
180
+ self.display = False
181
+ self._single_input.value = ""
182
+ self._multi_input.text = ""
183
+ self._multiline_mode = False
184
+ self._update_mode_display()
185
+ self._app.agent_state = "RUNNING"
186
+ self._app.update_content()
187
+ # Reset scroll position to bottom since input container disappearing changes layout
188
+ # somehow scroll_to doesn't work.
189
+ self._app._vscroll.scroll_y = 0
190
+ self._input_event.set()
191
+
192
+ def action_toggle_mode(self) -> None:
193
+ """Switch from single-line to multi-line mode (one-way only)."""
194
+ if self.pending_prompt is None or self._multiline_mode:
195
+ return
196
+
197
+ self._multiline_mode = True
198
+ self._update_mode_display()
199
+ self.on_focus()
200
+
201
+ def _update_mode_display(self) -> None:
202
+ """Update the display based on current mode."""
203
+ if self._multiline_mode:
204
+ self._multi_input.text = self._single_input.value
205
+ self._single_input.display = False
206
+ self._multi_input.display = True
207
+ self._hint_text.update(
208
+ "[reverse][bold][$accent] Ctrl+D [/][/][/] to submit, [reverse][bold][$accent] Tab [/][/][/] to switch focus with other controls"
209
+ )
210
+ else:
211
+ self._hint_text.update(
212
+ "[reverse][bold][$accent] Enter [/][/][/] to submit, [reverse][bold][$accent] Ctrl+T [/][/][/] to switch to multi-line input, [reverse][bold][$accent] Tab [/][/][/] to switch focus with other controls",
213
+ )
214
+ self._multi_input.display = False
215
+ self._single_input.display = True
216
+
217
+ def on_input_submitted(self, event: Input.Submitted) -> None:
218
+ """Handle single-line input submission."""
219
+ if not self._multiline_mode:
220
+ text = event.input.value.strip()
221
+ self._complete_input(text)
222
+
223
+ def on_key(self, event: Key) -> None:
224
+ """Handle key events."""
225
+ if event.key == "ctrl+t" and not self._multiline_mode:
226
+ event.prevent_default()
227
+ self.action_toggle_mode()
228
+ return
229
+
230
+ if self._multiline_mode and event.key == "ctrl+d":
231
+ event.prevent_default()
232
+ self._complete_input(self._multi_input.text.strip())
233
+ return
234
+
235
+ if event.key == "escape":
236
+ event.prevent_default()
237
+ self.can_focus = False
238
+ self._app.set_focus(None)
239
+ return
240
+
241
+
242
+ class TextualAgent(App):
243
+ BINDINGS = [
244
+ Binding("right,l", "next_step", "Step++", tooltip="Show next step of the agent"),
245
+ Binding("left,h", "previous_step", "Step--", tooltip="Show previous step of the agent"),
246
+ Binding("0", "first_step", "Step=0", tooltip="Show first step of the agent", show=False),
247
+ Binding("$", "last_step", "Step=-1", tooltip="Show last step of the agent", show=False),
248
+ Binding("j,down", "scroll_down", "Scroll down", show=False),
249
+ Binding("k,up", "scroll_up", "Scroll up", show=False),
250
+ Binding("q,ctrl+q", "quit", "Quit", tooltip="Quit the agent"),
251
+ Binding("y,ctrl+y", "yolo", "YOLO mode", tooltip="Switch to YOLO Mode (LM actions will execute immediately)"),
252
+ Binding(
253
+ "c",
254
+ "confirm",
255
+ "CONFIRM mode",
256
+ tooltip="Switch to Confirm Mode (LM proposes commands and you confirm/reject them)",
257
+ ),
258
+ Binding("u,ctrl+u", "human", "HUMAN mode", tooltip="Switch to Human Mode (you can now type commands directly)"),
259
+ Binding("f1,question_mark", "toggle_help_panel", "Help", tooltip="Show help"),
260
+ ]
261
+
262
+ def __init__(self, model, env, **kwargs):
263
+ css_path = os.environ.get("MSWEA_MINI_STYLE_PATH", str(Path(__file__).parent.parent / "config" / "mini.tcss"))
264
+ self.__class__.CSS = Path(css_path).read_text()
265
+ super().__init__()
266
+ self.agent_state = "UNINITIALIZED"
267
+ self.agent = _TextualAgent(self, model=model, env=env, **kwargs)
268
+ self._i_step = 0
269
+ self.n_steps = 1
270
+ self.input_container = SmartInputContainer(self)
271
+ self.log_handler = AddLogEmitCallback(lambda record: self.call_from_thread(self.on_log_message_emitted, record))
272
+ logging.getLogger().addHandler(self.log_handler)
273
+ self._spinner = Spinner("dots")
274
+ self.exit_status: str = "ExitStatusUnset"
275
+ self.result: str = ""
276
+
277
+ self._vscroll = VerticalScroll()
278
+
279
+ def run(self, task: str, **kwargs) -> tuple[str, str]:
280
+ threading.Thread(target=lambda: self.agent.run(task, **kwargs), daemon=True).start()
281
+ super().run()
282
+ return self.exit_status, self.result
283
+
284
+ # --- Basics ---
285
+
286
+ @property
287
+ def config(self):
288
+ return self.agent.config
289
+
290
+ @property
291
+ def i_step(self) -> int:
292
+ """Current step index."""
293
+ return self._i_step
294
+
295
+ @i_step.setter
296
+ def i_step(self, value: int) -> None:
297
+ """Set current step index, automatically clamping to valid bounds."""
298
+ if value != self._i_step:
299
+ self._i_step = max(0, min(value, self.n_steps - 1))
300
+ self._vscroll.scroll_to(y=0, animate=False)
301
+ self.update_content()
302
+
303
+ def compose(self) -> ComposeResult:
304
+ yield Header()
305
+ with Container(id="main"):
306
+ with self._vscroll:
307
+ with Vertical(id="content"):
308
+ pass
309
+ yield self.input_container
310
+ yield Footer()
311
+
312
+ def on_mount(self) -> None:
313
+ self.agent_state = "RUNNING"
314
+ self.update_content()
315
+ self.set_interval(1 / 8, self._update_headers)
316
+
317
+ @property
318
+ def messages(self) -> list[dict]:
319
+ return self.agent.messages
320
+
321
+ @property
322
+ def model(self):
323
+ return self.agent.model
324
+
325
+ @property
326
+ def env(self):
327
+ return self.agent.env
328
+
329
+ # --- Reacting to events ---
330
+
331
+ def on_message_added(self) -> None:
332
+ auto_follow = self.i_step == self.n_steps - 1 and self._vscroll.scroll_y <= 1
333
+ self.n_steps = len(_messages_to_steps(self.agent.messages))
334
+ self.update_content()
335
+ if auto_follow:
336
+ self.action_last_step()
337
+
338
+ def on_log_message_emitted(self, record: logging.LogRecord) -> None:
339
+ """Handle log messages of warning level or higher by showing them as notifications."""
340
+ if record.levelno >= logging.WARNING:
341
+ self.notify(f"[{record.levelname}] {record.getMessage()}", severity="warning")
342
+
343
+ def on_unmount(self) -> None:
344
+ """Clean up the log handler when the app shuts down."""
345
+ if hasattr(self, "log_handler"):
346
+ logging.getLogger().removeHandler(self.log_handler)
347
+
348
+ def on_agent_finished(self, exit_status: str, result: str):
349
+ self.agent_state = "STOPPED"
350
+ self.notify(f"Agent finished with status: {exit_status}")
351
+ self.exit_status = exit_status
352
+ self.result = result
353
+ self.update_content()
354
+
355
+ # --- UI update logic ---
356
+
357
+ def update_content(self) -> None:
358
+ container = self.query_one("#content", Vertical)
359
+ container.remove_children()
360
+ items = _messages_to_steps(self.agent.messages)
361
+
362
+ if not items:
363
+ container.mount(Static("Waiting for agent to start..."))
364
+ return
365
+
366
+ for message in items[self.i_step]:
367
+ if isinstance(message["content"], list):
368
+ content_str = "\n".join([item["text"] for item in message["content"]])
369
+ else:
370
+ content_str = str(message["content"])
371
+ message_container = Vertical(classes="message-container")
372
+ container.mount(message_container)
373
+ role = message["role"].replace("assistant", "mini-swe-agent")
374
+ message_container.mount(Static(role.upper(), classes="message-header"))
375
+ message_container.mount(Static(Text(content_str, no_wrap=False), classes="message-content"))
376
+
377
+ if self.input_container.pending_prompt is not None:
378
+ self.agent_state = "AWAITING_INPUT"
379
+ self.input_container.display = self.input_container.pending_prompt is not None and self.i_step == len(items) - 1
380
+ if self.input_container.display:
381
+ self.input_container.on_focus()
382
+
383
+ self._update_headers()
384
+ self.refresh()
385
+
386
+ def _update_headers(self) -> None:
387
+ """Update just the title with current state and spinner if needed."""
388
+ status_text = self.agent_state
389
+ if self.agent_state == "RUNNING":
390
+ spinner_frame = str(self._spinner.render(time.time())).strip()
391
+ status_text = f"{self.agent_state} {spinner_frame}"
392
+ self.title = f"Step {self.i_step + 1}/{self.n_steps} - {status_text} - Cost: ${self.agent.model.cost:.2f}"
393
+ try:
394
+ self.query_one("Header").set_class(self.agent_state == "RUNNING", "running")
395
+ except NoMatches: # might be called when shutting down
396
+ pass
397
+
398
+ # --- Other textual overrides ---
399
+
400
+ def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
401
+ # Add to palette
402
+ yield from super().get_system_commands(screen)
403
+ for binding in self.BINDINGS:
404
+ description = f"{binding.description} (shortcut {' OR '.join(binding.key.split(','))})" # type: ignore[attr-defined]
405
+ action_method = getattr(self, f"action_{binding.action}") # type: ignore[attr-defined]
406
+ yield SystemCommand(description, binding.tooltip, action_method) # type: ignore[attr-defined]
407
+
408
+ # --- Textual bindings ---
409
+
410
+ def action_yolo(self):
411
+ self.agent.config.mode = "yolo"
412
+ if self.input_container.pending_prompt is not None:
413
+ self.input_container._complete_input("") # accept
414
+ self.notify("YOLO mode enabled - LM actions will execute immediately")
415
+
416
+ def action_human(self):
417
+ if self.agent.config.mode == "confirm" and self.input_container.pending_prompt is not None:
418
+ self.input_container._complete_input("User switched to manual mode, this command will be ignored")
419
+ self.agent.config.mode = "human"
420
+ self.notify("Human mode enabled - you can now type commands directly")
421
+
422
+ def action_confirm(self):
423
+ if self.agent.config.mode == "human" and self.input_container.pending_prompt is not None:
424
+ self.input_container._complete_input("") # just submit blank action
425
+ self.agent.config.mode = "confirm"
426
+ self.notify("Confirm mode enabled - LM proposes commands and you confirm/reject them")
427
+
428
+ def action_next_step(self) -> None:
429
+ self.i_step += 1
430
+
431
+ def action_previous_step(self) -> None:
432
+ self.i_step -= 1
433
+
434
+ def action_first_step(self) -> None:
435
+ self.i_step = 0
436
+
437
+ def action_last_step(self) -> None:
438
+ self.i_step = self.n_steps - 1
439
+
440
+ def action_scroll_down(self) -> None:
441
+ self._vscroll.scroll_to(y=self._vscroll.scroll_target_y + 15)
442
+
443
+ def action_scroll_up(self) -> None:
444
+ self._vscroll.scroll_to(y=self._vscroll.scroll_target_y - 15)
445
+
446
+ def action_toggle_help_panel(self) -> None:
447
+ if self.query("HelpPanel"):
448
+ self.action_hide_help_panel()
449
+ else:
450
+ self.action_show_help_panel()
@@ -0,0 +1,10 @@
1
+ # Configs
2
+
3
+ * `mini.yaml` - Default config for `mini`/`agents/interactive.py` or `mini -v`/`agents/interactive_textual.py` agent.
4
+ * `mini_no_temp.yaml` - Same as `mini.yaml` but without the temperature setting
5
+ * `default.yaml` - Default config for the `default.py` agent.
6
+ * `github_issue.yaml` - Config for the `run/github_issue.py` entry point.
7
+
8
+ ## Extras
9
+
10
+ * `extra/swebench.yaml` - Config for the `run/extra/swebench.py` entry point.
@@ -0,0 +1,27 @@
1
+ """Configuration files and utilities for mini-SWE-agent."""
2
+
3
+ import os
4
+ from pathlib import Path
5
+
6
+ builtin_config_dir = Path(__file__).parent
7
+
8
+
9
+ def get_config_path(config_spec: str | Path) -> Path:
10
+ """Get the path to a config file."""
11
+ config_spec = Path(config_spec)
12
+ if config_spec.suffix != ".yaml":
13
+ config_spec = config_spec.with_suffix(".yaml")
14
+ candidates = [
15
+ Path(config_spec),
16
+ Path(os.getenv("MSWEA_CONFIG_DIR", ".")) / config_spec,
17
+ builtin_config_dir / config_spec,
18
+ builtin_config_dir / "extra" / config_spec,
19
+ ]
20
+ for candidate in candidates:
21
+ if candidate.exists():
22
+ return candidate
23
+
24
+ raise FileNotFoundError(f"Could not find config file for {config_spec} (tried: {candidates})")
25
+
26
+
27
+ __all__ = ["builtin_config_dir", "get_config_path"]