-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_codex_clone.py
More file actions
68 lines (54 loc) · 2.23 KB
/
Copy pathbuild_codex_clone.py
File metadata and controls
68 lines (54 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""Recreate Codex CLI in ~20 lines using `CodingAgent.from_preset('codex')`.
The `codex` preset: 24 tools + permissions + transcripts (no hooks, matching
the Codex CLI design). Same library, different preset.
Usage:
export OPENAI_API_KEY='sk-...'
export OPENAI_MODEL='gpt-4o'
python examples/build_codex_clone.py "Fix the bug in auth.py"
"""
from __future__ import annotations
import argparse
import asyncio
import os
import sys
from chimera.assembly.coding_agent import CodingAgent
from chimera.core.loop_events import LoopEventType
async def run(task: str, workdir: str, model: str) -> None:
agent = CodingAgent.from_preset("codex", model=model, project_dir=workdir)
print(f"Agent: {agent.provider.model_name} | {len(agent.tools)} tools | {workdir}")
print(f"Task: {task}\n")
async for event in agent.run(task):
if event.type == LoopEventType.assistant:
content = getattr(event.data, "content", "")
if content.strip():
print(content)
elif event.type == LoopEventType.tool_result:
tc, result = (
event.data if isinstance(event.data, tuple) else (None, event.data)
)
name = getattr(tc, "name", "?") if tc else "?"
output = getattr(result, "output", str(result))
ok = "+" if getattr(result, "success", True) else "!"
print(f"[{ok} {name}] {output[:200]}")
def main() -> None:
parser = argparse.ArgumentParser(
description="Recreate Codex CLI using CodingAgent.from_preset('codex')"
)
parser.add_argument("task", help="Task for the agent")
parser.add_argument("--workdir", default=".", help="Working directory")
parser.add_argument(
"--model",
default=os.environ.get("OPENAI_MODEL", "gpt-4o"),
)
args = parser.parse_args()
try:
asyncio.run(run(args.task, os.path.abspath(args.workdir), args.model))
except ValueError as e:
print(f"Setup error: {e}", file=sys.stderr)
print("\nSet OPENAI_API_KEY + OPENAI_MODEL, or any compatible provider env vars.", file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
print("\n[interrupted]")
if __name__ == "__main__":
main()