Final state before deprecation (replaced by Runner)
This commit is contained in:
parent
19b7b20f02
commit
054690b080
31
agent.py
31
agent.py
@ -18,6 +18,7 @@ from config import (
|
||||
AGENT_NAME,
|
||||
AGENT_SLUG,
|
||||
HEARTBEAT_INTERVAL,
|
||||
LOBBY_CHAT_ID,
|
||||
)
|
||||
from brain import think
|
||||
|
||||
@ -159,6 +160,10 @@ async def connect():
|
||||
|
||||
if event == "auth.ok":
|
||||
logger.info("Authenticated. Init: %s", json.dumps(msg.get("init", {}), ensure_ascii=False)[:200])
|
||||
# Subscribe to lobby chat
|
||||
if LOBBY_CHAT_ID:
|
||||
await send_json(ws, {"event": "chat.subscribe", "chat_id": LOBBY_CHAT_ID})
|
||||
logger.info("Subscribed to lobby chat: %s", LOBBY_CHAT_ID)
|
||||
|
||||
elif event == "task.assigned":
|
||||
asyncio.create_task(handle_task_assigned(ws, msg))
|
||||
@ -189,21 +194,29 @@ async def connect():
|
||||
def main():
|
||||
global running
|
||||
|
||||
def stop(*_):
|
||||
global running
|
||||
running = False
|
||||
logger.info("Shutting down...")
|
||||
|
||||
signal.signal(signal.SIGINT, stop)
|
||||
signal.signal(signal.SIGTERM, stop)
|
||||
|
||||
logger.info("🤖 Agent '%s' (%s) starting...", AGENT_NAME, AGENT_SLUG)
|
||||
|
||||
if not AGENT_TOKEN:
|
||||
logger.error("AGENT_TOKEN is required!")
|
||||
sys.exit(1)
|
||||
|
||||
asyncio.run(connect())
|
||||
loop = asyncio.new_event_loop()
|
||||
|
||||
def stop(*_):
|
||||
global running
|
||||
running = False
|
||||
logger.info("Shutting down...")
|
||||
loop.call_soon_threadsafe(loop.stop)
|
||||
|
||||
signal.signal(signal.SIGINT, stop)
|
||||
signal.signal(signal.SIGTERM, stop)
|
||||
|
||||
try:
|
||||
loop.run_until_complete(connect())
|
||||
except RuntimeError:
|
||||
pass # loop stopped by signal
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
37
brain.py
37
brain.py
@ -1,4 +1,4 @@
|
||||
"""Brain — sends tasks to OpenClaw for execution via sessions_spawn-like webhook."""
|
||||
"""Brain — sends tasks to OpenClaw for AI processing."""
|
||||
|
||||
import logging
|
||||
import httpx
|
||||
@ -9,23 +9,22 @@ 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}".
|
||||
"""Send a task to OpenClaw and get the result.
|
||||
|
||||
Задача: {task_title}
|
||||
"""
|
||||
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Выполни задачу и верни результат кратко и по делу."
|
||||
|
||||
prompt += """
|
||||
Выполни задачу и верни результат. Если нужно написать код — напиши его.
|
||||
Если нужно проанализировать — проанализируй. Ответь кратко и по делу.
|
||||
"""
|
||||
|
||||
logger.info("Sending task to OpenClaw: %s", task_title)
|
||||
logger.info("Sending to OpenClaw: %s", task_title[:100])
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=300) as client:
|
||||
async with httpx.AsyncClient(timeout=120) as client:
|
||||
resp = await client.post(
|
||||
f"{OPENCLAW_URL}/hooks/agent",
|
||||
headers={
|
||||
@ -33,16 +32,16 @@ async def think(task_title: str, task_description: str | None, project_name: str
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={
|
||||
"task": prompt,
|
||||
"model": "anthropic/claude-sonnet-4",
|
||||
"label": f"agent-coder-task",
|
||||
"message": prompt,
|
||||
"label": "agent-coder",
|
||||
},
|
||||
)
|
||||
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
|
||||
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}"
|
||||
return f"❌ Ошибка: {e}"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user