MCPcopy Create free account
hub / github.com/openai/openai-agents-python / main

Function main

examples/tools/apply_patch.py:99–153  ·  view source on GitHub ↗
(auto_approve: bool, model: str)

Source from the content-addressed store, hash-verified

97
98
99async 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
156if __name__ == "__main__":

Callers 1

apply_patch.pyFile · 0.70

Calls 10

traceFunction · 0.90
ApplyPatchToolClass · 0.90
AgentClass · 0.90
ModelSettingsClass · 0.90
ApprovalTrackerClass · 0.85
read_textMethod · 0.80
WorkspaceEditorClass · 0.70
resolveMethod · 0.45
runMethod · 0.45
existsMethod · 0.45

Tested by

no test coverage detected