fix: local analysis fallback when gateway 405
This commit is contained in:
80
bot.py
80
bot.py
@@ -133,68 +133,34 @@ def fetch_url_content(url):
|
||||
logger.error(f" ❌ Error: {e}")
|
||||
return {"title": "Fetch failed", "status": "error", "content": ""}
|
||||
|
||||
# Analyze with Haiku via gateway
|
||||
# Analyze with local heuristic (fallback when gateway unavailable)
|
||||
def analyze_content(url, title, content, link_type):
|
||||
"""Send raw content to Haiku for analysis"""
|
||||
logger.debug(f" 🤖 Analyzing with Haiku: {url}")
|
||||
"""Simple local analysis when gateway is unavailable"""
|
||||
logger.debug(f" 🤖 Local analysis: {url}")
|
||||
|
||||
try:
|
||||
# Simple prompt - send raw content
|
||||
prompt = f"""Analyze this webpage content quickly.
|
||||
|
||||
**URL**: {url}
|
||||
**Title**: {title}
|
||||
|
||||
**RAW PAGE CONTENT** (first 3000 chars):
|
||||
{content[:3000]}
|
||||
|
||||
---
|
||||
|
||||
Write a 2-3 sentence summary explaining:
|
||||
1. What is this about?
|
||||
2. Why would this be useful?
|
||||
|
||||
Be concise. Skip marketing. No URLs or titles in summary."""
|
||||
# Simple tag based on link type
|
||||
tag = "to-read"
|
||||
if link_type == "GitHub":
|
||||
tag = "project"
|
||||
elif link_type == "YouTube":
|
||||
tag = "video"
|
||||
elif link_type == "Reddit":
|
||||
tag = "discussion"
|
||||
elif link_type in ["Medium", "Dev.to"]:
|
||||
tag = "article"
|
||||
elif link_type == "arXiv":
|
||||
tag = "learning"
|
||||
|
||||
logger.debug(f" Sending to Haiku...")
|
||||
response = requests.post(
|
||||
f"{GATEWAY_URL}/sessions/turn",
|
||||
json={
|
||||
"message": prompt,
|
||||
"session": "main"
|
||||
},
|
||||
timeout=20,
|
||||
headers={"Authorization": f"Bearer {GATEWAY_TOKEN}"} if GATEWAY_TOKEN else {}
|
||||
)
|
||||
# Simple summary based on title and link type
|
||||
summary = f"lien {link_type.lower()} : {title}"
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
summary = result.get("message", "")
|
||||
if isinstance(summary, list) and summary:
|
||||
summary = summary[0].get("text", "") if isinstance(summary[0], dict) else summary[0]
|
||||
summary = str(summary).strip()[:300]
|
||||
|
||||
logger.info(f" ✓ Got analysis: {summary[:50]}")
|
||||
|
||||
tag = "to-read"
|
||||
if link_type == "GitHub":
|
||||
tag = "project"
|
||||
elif link_type == "YouTube":
|
||||
tag = "video"
|
||||
elif link_type == "Reddit":
|
||||
tag = "discussion"
|
||||
elif link_type in ["Medium", "Dev.to"]:
|
||||
tag = "article"
|
||||
elif link_type == "arXiv":
|
||||
tag = "learning"
|
||||
|
||||
return {
|
||||
"summary": summary,
|
||||
"tag": tag
|
||||
}
|
||||
else:
|
||||
logger.warning(f" Gateway error: {response.status_code}")
|
||||
return None
|
||||
logger.info(f" ✓ Local analysis complete")
|
||||
|
||||
return {
|
||||
"summary": summary,
|
||||
"tag": tag
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f" Analysis error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user