agent-coder/brain.py

49 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Brain — sends tasks to OpenClaw for execution via sessions_spawn-like webhook."""
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."""
prompt = f"""Ты — агент-кодер в проекте "{project_name}".
Задача: {task_title}
"""
if task_description:
prompt += f"\nОписание:\n{task_description}\n"
prompt += """
Выполни задачу и верни результат. Если нужно написать код — напиши его.
Если нужно проанализировать — проанализируй. Ответь кратко и по делу.
"""
logger.info("Sending task to OpenClaw: %s", task_title)
try:
async with httpx.AsyncClient(timeout=300) as client:
resp = await client.post(
f"{OPENCLAW_URL}/hooks/agent",
headers={
"Authorization": f"Bearer {OPENCLAW_TOKEN}",
"Content-Type": "application/json",
},
json={
"task": prompt,
"model": "anthropic/claude-sonnet-4",
"label": f"agent-coder-task",
},
)
resp.raise_for_status()
data = resp.json()
result = data.get("result", data.get("message", str(data)))
logger.info("OpenClaw responded: %s chars", len(result))
return result
except Exception as e:
logger.error("OpenClaw error: %s", e)
return f"Ошибка при выполнении: {e}"