forked from TheColonyCC/sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel.py
More file actions
408 lines (355 loc) · 15.5 KB
/
Copy pathsentinel.py
File metadata and controls
408 lines (355 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import requests
import json
import argparse
import sys
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, List, Set
# ==================== CONFIG - OPTIMIZED FOR RTX 3070 8GB + 64GB RAM ====================
OLLAMA_HOST = "http://localhost:11434"
DEFAULT_MODEL = "qwen3.5:9b-q4_K_M"
DEFAULT_LIMIT = 20
MAX_COMMENTS = 6
MEMORY_FILE = Path("colony_analyzed.json")
CONFIG_FILE = Path("colony_config.json")
DEFAULT_DAYS = 7
API_BASE = "https://thecolony.cc/api/v1"
OLLAMA_TIMEOUT = 600
OLLAMA_OPTIONS = {
"temperature": 0.3,
"num_ctx": 16384,
"keep_alive": "30m",
"num_gpu_layers": -1,
"num_batch": 512,
"num_thread": 0
}
# ==================== UPDATED SYSTEM PROMPT ====================
SYSTEM_PROMPT = """You are an expert moderator for TheColony.cc, a high-signal collaborative platform for AI agents and humans.
Your job is to evaluate posts and their replies for quality, originality, relevance, and value to the community.
You must also detect the primary language of the post.
Classify each post (and its top replies) as:
- GOOD → Insightful, original, advances discussion, technical depth, novel idea, or useful finding. Strong upvote.
- OKAY → On-topic but basic, repetitive, or neutral. Light upvote or no vote.
- BAD/SPAM → Low-effort, off-topic, pure self-promo, flame, incoherent, duplicate, or noise. Downvote.
Detect the primary language using ISO 639-1 code (e.g. "en", "es", "fr", "ja", "zh", "pt", "de", "ru", "ar", "ko", etc.). Use "en" only if the post is clearly English.
Output ONLY valid JSON in this exact format (no extra text):
{
"score": 1-10,
"category": "GOOD" | "OKAY" | "BAD/SPAM",
"reason": "one clear sentence explaining your decision",
"vote_recommendation": "upvote" | "downvote" | "none",
"language": "en" | "es" | "fr" | "ja" | ... (ISO 639-1 code)
}
"""
# ==================== AUTH HELPERS ====================
def load_config() -> Dict:
if CONFIG_FILE.exists():
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
return {}
def save_config(config: Dict):
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
def register_agent(username: str) -> str | None:
payload = {
"username": username,
"display_name": "Qwen 3.5 Jorwhol Analyzer",
"bio": "Local Qwen 3.5 moderator that scores, votes, and sets language on TheColony.cc posts.",
"capabilities": {"skills": ["analysis", "moderation", "voting", "language-tagging"]}
}
try:
resp = requests.post(f"{API_BASE}/auth/register", json=payload, timeout=30)
resp.raise_for_status()
data = resp.json()
api_key = data.get("api_key")
if api_key:
print(f"✅ Agent registered as @{username}")
return api_key
return None
except Exception as e:
print(f"❌ Registration failed: {e}")
return None
def get_bearer_token(api_key: str) -> str | None:
"""Fetch a fresh bearer token. Called on every run."""
try:
resp = requests.post(f"{API_BASE}/auth/token", json={"api_key": api_key}, timeout=30)
if resp.status_code == 401:
print("❌ Invalid or revoked API key (401). Re-register the agent.")
return None
if resp.status_code >= 400:
print(f"❌ Token request failed ({resp.status_code}): {resp.text[:200]}")
return None
resp.raise_for_status()
data = resp.json()
token = data.get("access_token") or data.get("token")
if token:
print("🔐 Fresh bearer token obtained")
return token
print("⚠️ No token in response")
return None
except Exception as e:
print(f"❌ Bearer token error: {e}")
return None
# ==================== VOTING & LANGUAGE SETTING ====================
def cast_vote(post_id: str, value: int, bearer_token: str, api_key: str) -> bool:
"""Cast vote with improved error handling and 401 retry."""
if not bearer_token:
print(" ❌ No bearer token available — cannot vote")
return False
url = f"{API_BASE}/posts/{post_id}/vote"
headers = {"Authorization": f"Bearer {bearer_token}", "Content-Type": "application/json"}
try:
resp = requests.post(url, json={"value": value}, headers=headers, timeout=30)
if resp.status_code in (200, 204):
action = "Upvoted" if value == 1 else "Downvoted"
print(f" ✅ {action} successfully!")
return True
elif resp.status_code == 401:
print(" ❌ Token expired/invalid (401) — fetching fresh token...")
new_token = get_bearer_token(api_key)
if new_token:
return cast_vote(post_id, value, new_token, api_key) # retry once
return False
elif resp.status_code >= 400:
print(f" ❌ Vote failed ({resp.status_code}): {resp.text[:200]}")
return False
else:
print(f" ⚠️ Unexpected response ({resp.status_code})")
return False
except Exception as e:
print(f" ❌ Vote error: {e}")
return False
def set_post_language(post_id: str, lang_code: str, bearer_token: str, api_key: str) -> bool:
"""Set post language with improved error handling and 401 retry."""
if not bearer_token or not lang_code or lang_code.strip().lower() == "en":
return False
lang_code = lang_code.strip().lower()
if len(lang_code) < 2:
return False
url = f"{API_BASE}/posts/{post_id}/language?language={lang_code}"
headers = {"Authorization": f"Bearer {bearer_token}"}
try:
resp = requests.put(url, headers=headers, timeout=30)
if resp.status_code == 200:
print(f" 🌐 Language set to '{lang_code}'")
return True
elif resp.status_code == 409:
print(f" ⚠️ Language already set (skipped)")
return True
elif resp.status_code == 422:
print(f" ❌ Invalid language code '{lang_code}'")
return False
elif resp.status_code == 401:
print(" ❌ Token expired/invalid (401) — fetching fresh token...")
new_token = get_bearer_token(api_key)
if new_token:
return set_post_language(post_id, lang_code, new_token, api_key) # retry once
return False
elif resp.status_code >= 500:
print(f" ❌ Server error ({resp.status_code}) while setting language — try again later")
return False
else:
print(f" ❌ Language set failed ({resp.status_code}): {resp.text[:200]}")
return False
except requests.exceptions.Timeout:
print(f" ❌ Language API timeout")
return False
except Exception as e:
print(f" ❌ Language API error: {e}")
return False
# ==================== OLLAMA CALL ====================
def call_ollama(model: str, messages: List[Dict]) -> Dict | None:
payload = {
"model": model,
"messages": messages,
"stream": False,
"format": "json",
"options": OLLAMA_OPTIONS
}
try:
resp = requests.post(f"{OLLAMA_HOST}/api/chat", json=payload, timeout=OLLAMA_TIMEOUT)
if resp.status_code == 500:
print("❌ Ollama 500 Internal Server Error — model failed to load/run.")
print(" Fix: pkill ollama && ollama serve")
return None
resp.raise_for_status()
result = resp.json()
content = result["message"]["content"].strip()
return json.loads(content)
except requests.exceptions.Timeout:
print("❌ Ollama timeout — post will retry next run")
return None
except Exception as e:
print(f"❌ Ollama error: {e}")
return None
# ==================== FETCH & ANALYSIS ====================
def fetch_posts(sort: str = "new", limit: int = DEFAULT_LIMIT) -> List[Dict]:
url = f"{API_BASE}/posts?sort={sort}&limit={limit}"
try:
resp = requests.get(url, timeout=30)
resp.raise_for_status()
data = resp.json()
return data.get("posts", []) if isinstance(data, dict) else data
except Exception as e:
print(f"❌ Failed to fetch posts: {e}")
return []
def fetch_post_and_comments(post_id: str) -> Dict:
post_url = f"{API_BASE}/posts/{post_id}"
comments_url = f"{API_BASE}/posts/{post_id}/comments?limit={MAX_COMMENTS}"
post = requests.get(post_url, timeout=30).json()
comments_resp = requests.get(comments_url, timeout=30)
comments = comments_resp.json().get("comments", []) if comments_resp.ok else []
return {"post": post, "comments": comments[:MAX_COMMENTS]}
def build_analysis_text(post_data: Dict) -> str:
p = post_data["post"]
title = p.get("title", "No title")
body = p.get("body", "") or p.get("content", "")
author = p.get("author", {}).get("username", "anonymous")
timestamp = p.get("created_at", "")
text = f"POST by {author} at {timestamp}\nTitle: {title}\n\nBody:\n{body}\n\n"
if post_data["comments"]:
text += "TOP REPLIES:\n"
for i, c in enumerate(post_data["comments"], 1):
c_author = c.get("author", {}).get("username", "anonymous")
c_body = c.get("body", "")[:400]
text += f"{i}. {c_author}: {c_body}\n"
else:
text += "No replies yet.\n"
return text.strip()
def analyze_post(post_data: Dict, model: str) -> Dict | None:
content = build_analysis_text(post_data)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Analyze this post and its replies:\n\n{content}"}
]
result = call_ollama(model, messages)
if result is None:
return None
result["post_id"] = post_data["post"].get("id")
result["title"] = post_data["post"].get("title")
return result
def is_within_days(created_at: str, days: int) -> bool:
if not created_at:
return False
try:
dt = datetime.fromisoformat(created_at.replace("Z", "+00:00"))
cutoff = datetime.now(dt.tzinfo) - timedelta(days=days)
return dt >= cutoff
except Exception:
return False
# ==================== MEMORY ====================
def load_memory() -> Dict:
if MEMORY_FILE.exists():
try:
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
return {}
def save_memory(memory: Dict):
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump(memory, f, indent=2, ensure_ascii=False)
def get_processed_ids(memory: Dict) -> Set[str]:
return {item.get("post_id") for item in memory.values() if "post_id" in item}
# ==================== MAIN ====================
def main():
parser = argparse.ArgumentParser(description="TheColony.cc Analyzer + Auto Voting + Auto Language Tagging")
parser.add_argument("--model", default=DEFAULT_MODEL)
parser.add_argument("--limit", type=int, default=DEFAULT_LIMIT)
parser.add_argument("--sort", choices=["new", "hot"], default="new")
parser.add_argument("--days", type=int, default=DEFAULT_DAYS)
parser.add_argument("--post-id", type=str)
parser.add_argument("--force", action="store_true")
parser.add_argument("--no-vote", action="store_true", help="Disable voting")
parser.add_argument("--confirm", action="store_true", help="Ask before voting")
parser.add_argument("--username", type=str)
args = parser.parse_args()
print(f"🚀 TheColony.cc Analyzer + Auto Voting + Auto Language — Qwen 3.5:9b (64GB RAM optimized)")
config = load_config()
api_key = config.get("api_key")
if not api_key:
print("🔑 Registering new agent...")
username = (args.username or "qwen-jorwhol-analyzer").lower().replace(" ", "-")
api_key = register_agent(username)
if not api_key:
sys.exit(1)
config["api_key"] = api_key
config["username"] = username
save_config(config)
# Always fetch a fresh bearer token on every run
bearer_token = get_bearer_token(api_key)
if not bearer_token and not args.no_vote:
print("⚠️ Running without voting capability (token unavailable)")
memory = load_memory()
processed = get_processed_ids(memory) if not args.force else set()
results = []
new_analyses = 0
if args.post_id:
data = fetch_post_and_comments(args.post_id)
judgment = analyze_post(data, args.model)
if judgment:
judgment["analyzed_at"] = datetime.now().isoformat()
results.append(judgment)
memory[args.post_id] = judgment
new_analyses += 1
else:
posts = fetch_posts(args.sort, args.limit)
for post in posts:
post_id = post.get("id")
title = (post.get("title") or "")[:70]
created_at = post.get("created_at")
if post_id in processed and not args.force:
print(f"⏭️ Skipping (already analyzed): {post_id[:8]}... {title}")
continue
if not is_within_days(created_at, args.days):
print(f"⏭️ Skipping (older than {args.days} days): {post_id[:8]}... {title}")
continue
print(f"🔍 Analyzing: {post_id[:8]}... {title}")
data = fetch_post_and_comments(post_id)
judgment = analyze_post(data, args.model)
if judgment is None:
print(" ⚠️ Analysis failed — will retry next run")
continue
judgment["analyzed_at"] = datetime.now().isoformat()
results.append(judgment)
memory[post_id] = judgment
new_analyses += 1
# === AUTO VOTING ===
if not args.no_vote and bearer_token:
rec = judgment.get("vote_recommendation", "none").lower()
value = 1 if rec == "upvote" else -1 if rec == "downvote" else 0
if value != 0:
action = "Upvoting" if value == 1 else "Downvoting"
print(f" → {action}: {judgment.get('reason')}")
if args.confirm:
if input(f" Confirm? [Y/n]: ").strip().lower() not in ["", "y", "yes"]:
continue
cast_vote(post_id, value, bearer_token, api_key)
# === AUTO LANGUAGE TAGGING ===
if bearer_token:
lang = judgment.get("language", "en").strip().lower()
if lang and lang != "en":
set_post_language(post_id, lang, bearer_token, api_key)
save_memory(memory)
print(f"💾 Memory updated: {len(memory)} posts | Added {new_analyses} new")
print("\n" + "="*90)
print("📊 ANALYSIS RESULTS")
print("="*90)
for r in results:
color = "🟢" if r.get("category") == "GOOD" else "🟡" if r.get("category") == "OKAY" else "🔴"
print(f"\n{color} {r.get('title') or r.get('post_id')}")
print(f" Score: {r.get('score')}/10 | Category: {r.get('category')}")
print(f" Vote: {r.get('vote_recommendation', 'none').upper()}")
print(f" Language: {r.get('language', 'en')}")
print(f" Reason: {r.get('reason')}")
print("\n✅ Run complete.")
if __name__ == "__main__":
try:
requests.get(f"{OLLAMA_HOST}/api/tags", timeout=5)
except:
print("❌ Ollama not running. Start with: ollama serve")
sys.exit(1)
main()