ctx: RunContext[Deps], file: str, existing_text: str, replacement_text: str
) -> str:
"""Replace the single, exact occurrence of ``existing_text`` in ``file``."""
path = _resolve(ctx.deps.work_dir, file)
if not path.is_file():
return f"Error: no such file: {file}"
text = path.read_text(encoding="utf-8")
count = text.count(existing_text)
if count == 0:
return f"Error: existing_text not found in {file}"
if count > 1:
return (
f"Error: existing_text appears {count} times in {file}; "
"add more surrounding context to make it unique"
)
path.write_text(text.replace(existing_text, replacement_text), encoding="utf-8")
return f"Edited {file}"
✏ הפעלת פקודת מערכת
הכלי האחרון, bash, מפעיל פקודת מערכת. פה יש לנו שני אתגרים:
1. עלינו לוודא שהפקודה לא תיתקע, ולכן אנחנו מגדירים timeout.
2. עלינו לוודא שהפקודה לא תחזיר פלט ארוך מדי. בניגוד לקובץ שנשאר על הדיסק, פלט של פקודה נעלם אחרי שהפעלנו אותה, ולכן הכלי כותב את הפלט לקובץ זמני ומעביר את שם הקובץ לסוכן. כך אם הפלט לא ארוך מדי הסוכן יוכל לקרוא אותו בערך שחוזר מהכלי. אם הפלט כן ארוך הסוכן יצטרך להפעיל את כלי
read_file אחרי הפעלת פקודת המערכת ולקרוא את ההמשך.זה קוד הכלי:
def bash(ctx: RunContext[Deps], command: str) -> str:
"""Run a shell command in the project directory and return its output.
Returns the first 10,000 chars of combined stdout+stderr. The full
output is saved to a temp file whose path is reported when truncated,
so it can be read in full later with read_file.
"""
try:
proc = subprocess.run(
command,
shell=True,
cwd=ctx.deps.work_dir,
capture_output=True,
text=True,
timeout=BASH_TIMEOUT,
)
except subprocess.TimeoutExpired:
return f"Error: command timed out after {BASH_TIMEOUT}s"
output = proc.stdout + proc.stderr
header = f"(exit code {proc.returncode})\n"
if len(output) <= MAX_OUTPUT:
return header + output
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".txt", prefix="minicoder-", encoding="utf-8"
) as fh:
fh.write(output)
tmp_path = fh.name
return (
header
+ output[:MAX_OUTPUT]
+ f"\n\n[output truncated; full output saved to {tmp_path} — "
"read it with read_file]"
)
✏ עכשיו אתם
1. הפעילו את הסוכן על המכונה שלכם ובנו בעזרתו משחק. האם הוא הצליח? נסו להחליף מודל ובדקו כיצד מודלים שונים מתמודדים עם המשימה.
2. הוסיפו לסוכן "זכרון" כך שהסוכן יכתוב פרטים חשובים שהוא מגלה על הקוד לקובץ טקסט. הוסיפו את קובץ הטקסט הזה לפרומפט באופן אוטומטי. האם הזכרון עוזר לסוכן לכתוב קוד טוב יותר?
3. הוסיפו תמיכה בניהול מספר שיחות במקביל. פקודת
/new פותחת שיחה חדשה, פקודת /list מראה את כל השיחות ופקודת /resume חוזרת לשיחה אחרת.