(auto_approve: bool, model: str)
| 97 | |
| 98 | |
| 99 | async def main(auto_approve: bool, model: str) -> None: |
| 100 | with trace("apply_patch_example"): |
| 101 | with tempfile.TemporaryDirectory(prefix="apply-patch-example-") as workspace: |
| 102 | workspace_path = Path(workspace).resolve() |
| 103 | approvals = ApprovalTracker() |
| 104 | editor = WorkspaceEditor(workspace_path, approvals, auto_approve) |
| 105 | tool = ApplyPatchTool(editor=editor) |
| 106 | previous_response_id: str | None = None |
| 107 | |
| 108 | agent = Agent( |
| 109 | name="Patch Assistant", |
| 110 | model=model, |
| 111 | instructions=( |
| 112 | f"You can edit files inside {workspace_path} using the apply_patch tool. " |
| 113 | "When modifying an existing file, include the file contents between " |
| 114 | "<BEGIN_FILES> and <END_FILES> in your prompt." |
| 115 | ), |
| 116 | tools=[tool], |
| 117 | model_settings=ModelSettings(tool_choice="required"), |
| 118 | ) |
| 119 | |
| 120 | print(f"[info] Workspace root: {workspace_path}") |
| 121 | print(f"[info] Using model: {model}") |
| 122 | print("[run] Creating tasks.md") |
| 123 | result = await Runner.run( |
| 124 | agent, |
| 125 | "Create tasks.md with a shopping checklist of 5 entries.", |
| 126 | previous_response_id=previous_response_id, |
| 127 | ) |
| 128 | previous_response_id = result.last_response_id |
| 129 | print(f"[run] Final response #1:\n{result.final_output}\n") |
| 130 | notes_path = workspace_path / "tasks.md" |
| 131 | if not notes_path.exists(): |
| 132 | raise RuntimeError(f"{notes_path} was not created by the apply_patch tool.") |
| 133 | updated_notes = notes_path.read_text(encoding="utf-8") |
| 134 | print("[file] tasks.md after creation:\n") |
| 135 | print(updated_notes) |
| 136 | |
| 137 | prompt = ( |
| 138 | "<BEGIN_FILES>\n" |
| 139 | f"===== tasks.md\n{updated_notes}\n" |
| 140 | "<END_FILES>\n" |
| 141 | "Check off the last two items from the file." |
| 142 | ) |
| 143 | print("\n[run] Updating tasks.md") |
| 144 | result2 = await Runner.run( |
| 145 | agent, |
| 146 | prompt, |
| 147 | previous_response_id=previous_response_id, |
| 148 | ) |
| 149 | print(f"[run] Final response #2:\n{result2.final_output}\n") |
| 150 | if not notes_path.exists(): |
| 151 | raise RuntimeError("tasks.md vanished unexpectedly before the second read.") |
| 152 | print("[file] Final tasks.md:\n") |
| 153 | print(notes_path.read_text(encoding="utf-8")) |
| 154 | |
| 155 | |
| 156 | if __name__ == "__main__": |
no test coverage detected