Update main.py
Browse files
main.py
CHANGED
|
@@ -6,14 +6,22 @@ import uvicorn
|
|
| 6 |
from playwright.async_api import async_playwright
|
| 7 |
from openclaw import Agent, Tool
|
| 8 |
|
|
|
|
| 9 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 10 |
SPACE_HOST = os.getenv("SPACE_HOST")
|
| 11 |
STATIC_DIR = "static"
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 15 |
|
|
|
|
| 16 |
async def take_screenshot(url: str) -> str:
|
|
|
|
| 17 |
filepath = f"{STATIC_DIR}/screenshot.png"
|
| 18 |
async with async_playwright() as p:
|
| 19 |
browser = await p.chromium.launch(headless=True)
|
|
@@ -21,28 +29,53 @@ async def take_screenshot(url: str) -> str:
|
|
| 21 |
await page.goto(url)
|
| 22 |
await page.screenshot(path=filepath, full_page=True)
|
| 23 |
await browser.close()
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
tools = [Tool(name="take_screenshot", func=take_screenshot, description="Takes a screenshot of a URL.")]
|
| 27 |
|
|
|
|
| 28 |
agent = Agent(
|
| 29 |
model="gemini/gemini-2.5-pro",
|
| 30 |
tools=tools,
|
| 31 |
-
system_prompt=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
model_kwargs={"temperature": 0.2, "max_tokens": 8192, "top_p": 0.95}
|
| 33 |
)
|
| 34 |
|
|
|
|
| 35 |
@app.get("/health")
|
| 36 |
-
async def
|
|
|
|
| 37 |
|
| 38 |
-
@app.
|
| 39 |
async def wa_webhook(request: Request):
|
| 40 |
-
|
| 41 |
-
user_msg =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if user_msg.lower().startswith("claw "):
|
| 43 |
prompt = user_msg[5:].strip()
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return {"replies": []}
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 6 |
from playwright.async_api import async_playwright
|
| 7 |
from openclaw import Agent, Tool
|
| 8 |
|
| 9 |
+
# --- CONFIG ---
|
| 10 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
SPACE_HOST = os.getenv("SPACE_HOST")
|
| 12 |
STATIC_DIR = "static"
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
# Create static directory if it doesn't exist
|
| 17 |
+
if not os.path.exists(STATIC_DIR):
|
| 18 |
+
os.makedirs(STATIC_DIR)
|
| 19 |
+
|
| 20 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 21 |
|
| 22 |
+
# --- TOOLS ---
|
| 23 |
async def take_screenshot(url: str) -> str:
|
| 24 |
+
"""Navigates to a URL and takes a screenshot. Returns the URL to view it."""
|
| 25 |
filepath = f"{STATIC_DIR}/screenshot.png"
|
| 26 |
async with async_playwright() as p:
|
| 27 |
browser = await p.chromium.launch(headless=True)
|
|
|
|
| 29 |
await page.goto(url)
|
| 30 |
await page.screenshot(path=filepath, full_page=True)
|
| 31 |
await browser.close()
|
| 32 |
+
|
| 33 |
+
return f"Screenshot successful. View it here: https://{SPACE_HOST}/static/screenshot.png"
|
| 34 |
|
| 35 |
tools = [Tool(name="take_screenshot", func=take_screenshot, description="Takes a screenshot of a URL.")]
|
| 36 |
|
| 37 |
+
# --- AGENT ---
|
| 38 |
agent = Agent(
|
| 39 |
model="gemini/gemini-2.5-pro",
|
| 40 |
tools=tools,
|
| 41 |
+
system_prompt=(
|
| 42 |
+
"You are an expert coding/devops agent. Users interact via a WhatsApp bridge. "
|
| 43 |
+
"Keep replies concise and under 1000 chars. Explain before acting. "
|
| 44 |
+
"If you take a screenshot, provide the URL clearly."
|
| 45 |
+
),
|
| 46 |
model_kwargs={"temperature": 0.2, "max_tokens": 8192, "top_p": 0.95}
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# --- ENDPOINTS ---
|
| 50 |
@app.get("/health")
|
| 51 |
+
async def health_check():
|
| 52 |
+
return {"status": "alive"}
|
| 53 |
|
| 54 |
+
@app.api_route("/webhook", methods=["GET", "POST"])
|
| 55 |
async def wa_webhook(request: Request):
|
| 56 |
+
"""Universal webhook that handles both GET and POST requests."""
|
| 57 |
+
user_msg = ""
|
| 58 |
+
|
| 59 |
+
# Try to get message from URL (GET)
|
| 60 |
+
user_msg = request.query_params.get("message", "")
|
| 61 |
+
|
| 62 |
+
# Try to get message from JSON Body (POST)
|
| 63 |
+
if request.method == "POST":
|
| 64 |
+
try:
|
| 65 |
+
data = await request.json()
|
| 66 |
+
user_msg = data.get("message", user_msg)
|
| 67 |
+
except:
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
# Process prompt if it starts with "claw "
|
| 71 |
if user_msg.lower().startswith("claw "):
|
| 72 |
prompt = user_msg[5:].strip()
|
| 73 |
+
try:
|
| 74 |
+
response_text = await agent.arun(prompt)
|
| 75 |
+
return {"replies": [{"message": response_text}]}
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return {"replies": [{"message": f"Agent Error: {str(e)}"}]}
|
| 78 |
+
|
| 79 |
return {"replies": []}
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|