-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding_agent_minimal.py
More file actions
87 lines (72 loc) · 2.52 KB
/
Copy pathcoding_agent_minimal.py
File metadata and controls
87 lines (72 loc) · 2.52 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""Minimal coding agent with REPL -- under 80 lines of logic.
A stripped-down coding agent that demonstrates the core Chimera loop:
provider + tools + REPL. No argparse, no wire, no sessions -- just the
essentials.
Usage:
export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="your-token"
export ANTHROPIC_MODEL="glm-5"
python examples/coding_agent_minimal.py # current directory
python examples/coding_agent_minimal.py /tmp/project # specific directory
"""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import chimera
def main():
try:
provider = chimera.create_provider()
except ValueError as e:
print(f"Setup error: {e}\n")
print("Set one of these before running:")
print(" export ANTHROPIC_API_KEY='sk-ant-...'")
print(" # or for compatible endpoints:")
print(" export ANTHROPIC_BASE_URL='https://api.z.ai/api/anthropic'")
print(" export ANTHROPIC_AUTH_TOKEN='your-token'")
print(" export ANTHROPIC_MODEL='glm-5'")
sys.exit(1)
workdir = sys.argv[1] if len(sys.argv) > 1 else "."
workdir = os.path.abspath(workdir)
os.makedirs(workdir, exist_ok=True)
env = chimera.LocalEnvironment(workdir=workdir)
env.setup()
agent = chimera.Agent(
provider=provider,
tools=list(chimera.AGENT_TOOLS),
loop=chimera.ReAct(max_steps=20),
)
print(f"Chimera | {provider.model_name} | {workdir} | /help")
total = 0.0
while True:
try:
inp = input("\n> ").strip()
except (EOFError, KeyboardInterrupt):
break
if not inp:
continue
if inp == "/exit":
break
if inp == "/help":
print(" Type a task. Commands: /tools /cost /exit")
continue
if inp == "/tools":
for t in agent.tools:
print(f" {t.name}")
continue
if inp == "/cost":
print(f" ${total:.4f}")
continue
try:
result = agent.run(inp, env=env)
total += result.cost
print(f"\n[{result.steps} steps, ${result.cost:.4f}]")
except KeyboardInterrupt:
print("\n (interrupted)")
except Exception as e:
print(f"\n Error: {e}")
print(f"\nTotal: ${total:.4f}")
env.cleanup()
if __name__ == "__main__":
main()