agent-coder/brain.py

48 lines
1.7 KiB
Python

"""Brain — sends tasks to OpenClaw for AI processing."""
import logging
import httpx
from config import OPENCLAW_URL, OPENCLAW_TOKEN
logger = logging.getLogger("agent.brain")
async def think(task_title: str, task_description: str | None, project_name: str) -> str:
"""Send a task to OpenClaw and get the result.
Uses /hooks/wake to inject a system event into the main session
and waits for the response.
For now: fire-and-forget via /hooks/agent, returns acknowledgement.
"""
prompt = f"Ты — агент-кодер в проекте \"{project_name}\".\n\n"
prompt += f"Задача: {task_title}\n"
if task_description:
prompt += f"\nОписание:\n{task_description}\n"
prompt += "\nВыполни задачу и верни результат кратко и по делу."
logger.info("Sending to OpenClaw: %s", task_title[:100])
try:
async with httpx.AsyncClient(timeout=120) as client:
resp = await client.post(
f"{OPENCLAW_URL}/hooks/agent",
headers={
"Authorization": f"Bearer {OPENCLAW_TOKEN}",
"Content-Type": "application/json",
},
json={
"message": prompt,
"label": "agent-coder",
},
)
resp.raise_for_status()
data = resp.json()
run_id = data.get("runId", "unknown")
logger.info("OpenClaw run started: %s", run_id)
return f"🤖 Задача принята, обрабатываю... (run: {run_id[:8]})"
except Exception as e:
logger.error("OpenClaw error: %s", e)
return f"❌ Ошибка: {e}"