-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_runner.py
More file actions
91 lines (70 loc) · 2.46 KB
/
Copy pathagent_runner.py
File metadata and controls
91 lines (70 loc) · 2.46 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
88
89
90
91
import logging
import os
import re
import shutil
import subprocess
import tempfile
logger = logging.getLogger(__name__)
_TOKEN_USAGE_RE = re.compile(r"tokens used\s*[:=]?\s*([\d,]+)", re.IGNORECASE)
def _ensure_cli_available(cli_name):
if shutil.which(cli_name) is None:
raise FileNotFoundError(f"Required CLI '{cli_name}' not found in PATH")
def _run_with_output_file(cmd, prompt, repo_dir, config):
output_dir = config.data_dir
os.makedirs(output_dir, exist_ok=True)
with tempfile.NamedTemporaryFile(
mode="w",
delete=False,
dir=output_dir,
suffix=".log",
encoding="utf-8",
) as output_file:
output_path = output_file.name
with open(output_path, "w", encoding="utf-8") as output_handle:
result = subprocess.run(
cmd,
cwd=repo_dir,
input=prompt,
text=True,
stdout=output_handle,
stderr=subprocess.PIPE,
)
return result, output_path
def _log_token_usage(stderr):
if not stderr:
return
match = _TOKEN_USAGE_RE.search(stderr)
if not match:
return
try:
token_count = int(match.group(1).replace(",", ""))
except ValueError:
return
logger.info("Codex token usage: %s tokens", f"{token_count:,}")
def run_kilocode(prompt, repo_dir, config):
_ensure_cli_available("kilocode")
cmd = ["kilocode"] + config.kilocode_args
logger.info("Running kilocode with args: %s", " ".join(cmd))
result, output_path = _run_with_output_file(cmd, prompt, repo_dir, config)
return result, output_path
def run_codex(prompt, repo_dir, config):
_ensure_cli_available("codex")
cmd = ["codex", "exec", "-C", repo_dir] + config.codex_exec_args
if config.codex_model:
cmd += ["-m", config.codex_model]
if config.codex_prompt_mode == "stdin":
cmd += ["-"]
input_prompt = prompt
else:
cmd += [prompt]
input_prompt = None
logger.info("Running codex with args: %s", " ".join(cmd))
result, output_path = _run_with_output_file(cmd, input_prompt, repo_dir, config)
_log_token_usage(result.stderr)
return result, output_path
def run_agent(prompt, repo_dir, config):
if config.agent_cli == "kilocode":
return run_kilocode(prompt, repo_dir, config)
if config.agent_cli == "codex":
return run_codex(prompt, repo_dir, config)
raise ValueError(f"Unsupported AGENT_CLI '{config.agent_cli}'")