- verifier.py: syntax check via ast.parse, verify_function_present - queue.py: QUEUE.md parser, status markers [done]/[failed:N]/[skipped] - driver.py: full run loop — extract, prompt, generate, verify, splice, commit - EDIT/CREATE/DELETE/TEST actions - retry logic (max 3), auto-commit per task, cost tracking - cli.py: run/status/plan wired up; plan calls big model to decompose features - QUEUE.md: example queue with commented-out demo tasks - pyproject.toml: fix build backend tinyclaw run --dry-run works end-to-end
103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
"""Model API client for OpenRouter and OpenAI-compatible endpoints."""
|
|
|
|
import json
|
|
import os
|
|
import requests
|
|
from dataclasses import dataclass
|
|
|
|
|
|
OPENROUTER_BASE = "https://openrouter.ai/api/v1"
|
|
|
|
# Budget models that work well for code edits
|
|
RECOMMENDED_MODELS = {
|
|
"qwen3.5-1.5b": "qwen/qwen3.5-1.5b",
|
|
"qwen2.5-coder-3b": "qwen/qwen2.5-coder-3b-instruct",
|
|
"phi-3.5-mini": "microsoft/phi-3.5-mini-128k-instruct",
|
|
"llama-3.2-3b": "meta-llama/llama-3.2-3b-instruct",
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class ModelResponse:
|
|
"""Response from a model API call."""
|
|
content: str
|
|
model: str
|
|
prompt_tokens: int
|
|
completion_tokens: int
|
|
cost: float | None = None
|
|
|
|
|
|
def generate(
|
|
prompt: str,
|
|
model: str = "qwen/qwen3.5-1.5b",
|
|
system: str = "",
|
|
api_base: str | None = None,
|
|
api_key: str | None = None,
|
|
max_tokens: int = 1024,
|
|
temperature: float = 0.1,
|
|
) -> ModelResponse:
|
|
"""Send a prompt to a model and get a response.
|
|
|
|
Supports OpenRouter (default) and any OpenAI-compatible endpoint.
|
|
|
|
API key resolution:
|
|
1. api_key parameter
|
|
2. OPENROUTER_API_KEY env var (for OpenRouter)
|
|
3. TINYCLAW_API_KEY env var (for custom endpoints)
|
|
"""
|
|
base = api_base or OPENROUTER_BASE
|
|
key = api_key or os.environ.get("OPENROUTER_API_KEY") or os.environ.get("TINYCLAW_API_KEY")
|
|
|
|
if not key and "openrouter" in base:
|
|
raise ValueError("No API key. Set OPENROUTER_API_KEY or pass --api-key")
|
|
|
|
messages = []
|
|
if system:
|
|
messages.append({"role": "system", "content": system})
|
|
messages.append({"role": "user", "content": prompt})
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"HTTP-Referer": "https://git.rewr.ca/Pizza/TinyClaw",
|
|
"X-Title": "TinyClaw",
|
|
}
|
|
if key:
|
|
headers["Authorization"] = f"Bearer {key}"
|
|
|
|
payload = {
|
|
"model": model,
|
|
"messages": messages,
|
|
"max_tokens": max_tokens,
|
|
"temperature": temperature,
|
|
"stream": False,
|
|
}
|
|
|
|
resp = requests.post(
|
|
f"{base.rstrip('/')}/chat/completions",
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=60,
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
|
|
choice = data["choices"][0]
|
|
usage = data.get("usage", {})
|
|
|
|
return ModelResponse(
|
|
content=choice["message"]["content"],
|
|
model=data.get("model", model),
|
|
prompt_tokens=usage.get("prompt_tokens", 0),
|
|
completion_tokens=usage.get("completion_tokens", 0),
|
|
cost=_parse_cost(data),
|
|
)
|
|
|
|
|
|
def _parse_cost(data: dict) -> float | None:
|
|
"""Extract cost from OpenRouter response if available."""
|
|
# OpenRouter includes cost in usage or x-openrouter headers
|
|
usage = data.get("usage", {})
|
|
if "total_cost" in usage:
|
|
return float(usage["total_cost"])
|
|
return None
|