|
4 | 4 | import shutil |
5 | 5 | import subprocess |
6 | 6 | import sys |
7 | | -import textwrap |
8 | 7 | import urllib.error |
9 | 8 | import urllib.request |
10 | 9 | import uuid |
|
22 | 21 | "\\\\ \\|/ /", |
23 | 22 | "`-----'__", |
24 | 23 | ) |
25 | | -HELP_DETAILS = textwrap.dedent( |
26 | | - """\ |
27 | | - Commands: |
28 | | - /help Show this help message. |
29 | | - /memory Show the agent's distilled working memory. |
30 | | - /session Show the path to the saved session file. |
31 | | - /reset Clear the current session history and memory. |
32 | | - /exit Exit the agent. |
33 | | - """ |
34 | | -).strip() |
| 24 | +HELP_DETAILS = "\n".join( |
| 25 | + [ |
| 26 | + "Commands:", |
| 27 | + "/help Show this help message.", |
| 28 | + "/memory Show the agent's distilled working memory.", |
| 29 | + "/session Show the path to the saved session file.", |
| 30 | + "/reset Clear the current session history and memory.", |
| 31 | + "/exit Exit the agent.", |
| 32 | + ] |
| 33 | +) |
35 | 34 | MAX_TOOL_OUTPUT = 4000 |
36 | 35 | MAX_HISTORY = 12000 |
37 | 36 | IGNORED_PATH_NAMES = {".git", ".mini-coding-agent", "__pycache__", ".pytest_cache", ".ruff_cache", ".venv", "venv"} |
@@ -126,21 +125,19 @@ def git(args, fallback=""): |
126 | 125 | def text(self): |
127 | 126 | commits = "\n".join(f"- {line}" for line in self.recent_commits) or "- none" |
128 | 127 | docs = "\n".join(f"- {path}\n{snippet}" for path, snippet in self.project_docs.items()) or "- none" |
129 | | - return textwrap.dedent( |
130 | | - f"""\ |
131 | | - Workspace: |
132 | | - - cwd: {self.cwd} |
133 | | - - repo_root: {self.repo_root} |
134 | | - - branch: {self.branch} |
135 | | - - default_branch: {self.default_branch} |
136 | | - - status: |
137 | | - {self.status} |
138 | | - - recent_commits: |
139 | | - {commits} |
140 | | - - project_docs: |
141 | | - {docs} |
142 | | - """ |
143 | | - ).strip() |
| 128 | + return "\n".join([ |
| 129 | + "Workspace:", |
| 130 | + f"- cwd: {self.cwd}", |
| 131 | + f"- repo_root: {self.repo_root}", |
| 132 | + f"- branch: {self.branch}", |
| 133 | + f"- default_branch: {self.default_branch}", |
| 134 | + "- status:", |
| 135 | + self.status, |
| 136 | + "- recent_commits:", |
| 137 | + commits, |
| 138 | + "- project_docs:", |
| 139 | + docs, |
| 140 | + ]) |
144 | 141 |
|
145 | 142 |
|
146 | 143 | ############################## |
@@ -350,49 +347,42 @@ def build_prefix(self): |
350 | 347 | "<final>Done.</final>", |
351 | 348 | ] |
352 | 349 | ) |
353 | | - return textwrap.dedent( |
354 | | - f"""\ |
355 | | - You are Mini-Coding-Agent, a small local coding agent running through Ollama. |
356 | | -
|
357 | | - Rules: |
358 | | - - Use tools instead of guessing about the workspace. |
359 | | - - Return exactly one <tool>...</tool> or one <final>...</final>. |
360 | | - - Tool calls must look like: |
361 | | - <tool>{{"name":"tool_name","args":{{...}}}}</tool> |
362 | | - - For write_file and patch_file with multi-line text, prefer XML style: |
363 | | - <tool name="write_file" path="file.py"><content>...</content></tool> |
364 | | - - Final answers must look like: |
365 | | - <final>your answer</final> |
366 | | - - Never invent tool results. |
367 | | - - Keep answers concise and concrete. |
368 | | - - If the user asks you to create or update a specific file and the path is clear, use write_file or patch_file instead of repeatedly listing files. |
369 | | - - Before writing tests for existing code, read the implementation first. |
370 | | - - When writing tests, match the current implementation unless the user explicitly asked you to change the code. |
371 | | - - New files should be complete and runnable, including obvious imports. |
372 | | - - Do not repeat the same tool call with the same arguments if it did not help. Choose a different tool or return a final answer. |
373 | | - - Required tool arguments must not be empty. Do not call read_file, write_file, patch_file, run_shell, or delegate with args={{}}. |
374 | | -
|
375 | | - Tools: |
376 | | - {tool_text} |
377 | | -
|
378 | | - Valid response examples: |
379 | | - {examples} |
380 | | -
|
381 | | - {self.workspace.text()} |
382 | | - """ |
383 | | - ).strip() |
| 350 | + rules = "\n".join([ |
| 351 | + "- Use tools instead of guessing about the workspace.", |
| 352 | + "- Return exactly one <tool>...</tool> or one <final>...</final>.", |
| 353 | + "- Tool calls must look like:", |
| 354 | + ' <tool>{"name":"tool_name","args":{...}}</tool>', |
| 355 | + "- For write_file and patch_file with multi-line text, prefer XML style:", |
| 356 | + ' <tool name="write_file" path="file.py"><content>...</content></tool>', |
| 357 | + "- Final answers must look like:", |
| 358 | + " <final>your answer</final>", |
| 359 | + "- Never invent tool results.", |
| 360 | + "- Keep answers concise and concrete.", |
| 361 | + "- If the user asks you to create or update a specific file and the path is clear, use write_file or patch_file instead of repeatedly listing files.", |
| 362 | + "- Before writing tests for existing code, read the implementation first.", |
| 363 | + "- When writing tests, match the current implementation unless the user explicitly asked you to change the code.", |
| 364 | + "- New files should be complete and runnable, including obvious imports.", |
| 365 | + "- Do not repeat the same tool call with the same arguments if it did not help. Choose a different tool or return a final answer.", |
| 366 | + "- Required tool arguments must not be empty. Do not call read_file, write_file, patch_file, run_shell, or delegate with args={}.", |
| 367 | + ]) |
| 368 | + return "\n\n".join([ |
| 369 | + "You are Mini-Coding-Agent, a small local coding agent running through Ollama.", |
| 370 | + "Rules:\n" + rules, |
| 371 | + "Tools:\n" + tool_text, |
| 372 | + "Valid response examples:\n" + examples, |
| 373 | + self.workspace.text(), |
| 374 | + ]) |
384 | 375 |
|
385 | 376 | def memory_text(self): |
386 | 377 | memory = self.session["memory"] |
387 | | - return textwrap.dedent( |
388 | | - f"""\ |
389 | | - Memory: |
390 | | - - task: {memory['task'] or "-"} |
391 | | - - files: {", ".join(memory["files"]) or "-"} |
392 | | - - notes: |
393 | | - {chr(10).join(f"- {note}" for note in memory["notes"]) or "- none"} |
394 | | - """ |
395 | | - ).strip() |
| 378 | + notes = "\n".join(f"- {note}" for note in memory["notes"]) or "- none" |
| 379 | + return "\n".join([ |
| 380 | + "Memory:", |
| 381 | + f"- task: {memory['task'] or '-'}", |
| 382 | + f"- files: {', '.join(memory['files']) or '-'}", |
| 383 | + "- notes:", |
| 384 | + notes, |
| 385 | + ]) |
396 | 386 |
|
397 | 387 | ##################################################### |
398 | 388 | #### 4) Context Reduction And Output Management ##### |
@@ -430,19 +420,12 @@ def history_text(self): |
430 | 420 | #### 2) Prompt Shape And Cache Reuse (Continued) ####### |
431 | 421 | ######################################################## |
432 | 422 | def prompt(self, user_message): |
433 | | - return textwrap.dedent( |
434 | | - f"""\ |
435 | | - {self.prefix} |
436 | | -
|
437 | | - {self.memory_text()} |
438 | | -
|
439 | | - Transcript: |
440 | | - {self.history_text()} |
441 | | -
|
442 | | - Current user request: |
443 | | - {user_message} |
444 | | - """ |
445 | | - ).strip() |
| 423 | + return "\n\n".join([ |
| 424 | + self.prefix, |
| 425 | + self.memory_text(), |
| 426 | + "Transcript:\n" + self.history_text(), |
| 427 | + "Current user request:\n" + user_message, |
| 428 | + ]) |
446 | 429 |
|
447 | 430 | ############################################### |
448 | 431 | #### 5) Session Memory (Continued) ########### |
@@ -825,15 +808,15 @@ def tool_run_shell(self, args): |
825 | 808 | text=True, |
826 | 809 | timeout=timeout, |
827 | 810 | ) |
828 | | - return textwrap.dedent( |
829 | | - f"""\ |
830 | | - exit_code: {result.returncode} |
831 | | - stdout: |
832 | | - {result.stdout.strip() or "(empty)"} |
833 | | - stderr: |
834 | | - {result.stderr.strip() or "(empty)"} |
835 | | - """ |
836 | | - ).strip() |
| 811 | + return "\n".join( |
| 812 | + [ |
| 813 | + f"exit_code: {result.returncode}", |
| 814 | + "stdout:", |
| 815 | + result.stdout.strip() or "(empty)", |
| 816 | + "stderr:", |
| 817 | + result.stderr.strip() or "(empty)", |
| 818 | + ] |
| 819 | + ) |
837 | 820 |
|
838 | 821 | def tool_write_file(self, args): |
839 | 822 | path = self.path(args["path"]) |
|
0 commit comments