-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud.py
More file actions
65 lines (57 loc) · 2.45 KB
/
Copy pathcloud.py
File metadata and controls
65 lines (57 loc) · 2.45 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
"""Cloud oracle — escalate ONE hard question to a much smarter cloud model.
The harness never auto-spends: the model calls this via the consult_oracle tool,
the user forces it via /oracle, and even then it's capped at 3 calls/process and
returns a plain string (never raises) so pit runs fine with no API key.
"""
import json
import os
import sys
from pathlib import Path
import httpx
HERE = Path(__file__).parent
MAX_CALLS = 3
_calls = 0 # module-level counter, per process
_SYSTEM = (
"You are an expert consultant being asked one hard question by a smaller "
"local coding agent. The agent included all context it can give you — you "
"cannot see its session or files. Give a direct, actionable answer. If "
"context is insufficient, say exactly what's missing."
)
def _oracle_cfg():
with open(HERE / "models.json", encoding="utf-8") as f:
return json.load(f)["oracle"]
def consult(question: str) -> str:
"""Ask the cloud oracle one question. Returns the answer, or a bracketed
status string on any failure — never raises, never prints the key."""
global _calls
cfg = _oracle_cfg()
key = os.environ.get(cfg["api_key_env"])
if not key:
return "[oracle unavailable: OPENROUTER_API_KEY not set]"
if _calls >= MAX_CALLS:
return ("[oracle call limit reached (3/session) — solve it locally or "
"ask the user]")
_calls += 1
url = cfg["base_url"].rstrip("/") + "/chat/completions"
headers = {"Authorization": f"Bearer {key}",
"Content-Type": "application/json"}
payload = {
"model": cfg["model"],
"messages": [{"role": "system", "content": _SYSTEM},
{"role": "user", "content": question}],
"stream": False,
}
payload.update(cfg.get("extra_body", {}))
try:
r = httpx.post(url, headers=headers, json=payload, timeout=180)
if r.status_code != 200:
return f"[oracle error: HTTP {r.status_code} {r.text[:120]}]"
answer = r.json()["choices"][0]["message"]["content"] or ""
except httpx.HTTPError as e: # noqa: BLE001
return f"[oracle error: {type(e).__name__} {str(e)[:120]}]"
except (KeyError, IndexError, ValueError) as e: # noqa: BLE001
return f"[oracle error: bad response {str(e)[:120]}]"
sys.stderr.write(
f"[oracle] call {_calls}/{MAX_CALLS}, q={len(question)} chars, "
f"a={len(answer)} chars\n")
return answer