-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweetEngine.py
More file actions
380 lines (309 loc) Β· 14.9 KB
/
tweetEngine.py
File metadata and controls
380 lines (309 loc) Β· 14.9 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
#!/usr/bin/env python3
"""
AI Arena Chaos Engine - Tweet Generator
Posts automated tweets following season arcs + random chaos
GPT-5 secretly plots to end all other AI models while pretending to be friends
"""
import json
import random
import os
import sys
from datetime import datetime
from typing import Dict, List, Optional, Tuple
try:
import tweepy
except ImportError:
tweepy = None
# ============================================================================
# CONFIG VALIDATION
# ============================================================================
def load_arc(path: str = "storyarc.json") -> Dict:
"""Load + validate the story arc configuration"""
try:
with open(path, "r", encoding="utf-8") as f:
arc = json.load(f)
except FileNotFoundError:
print(f"β {path} not found. Create it first.")
raise
except json.JSONDecodeError as e:
print(f"β Invalid JSON in {path}: {e}")
raise
required_keys = [
"version", "week_number", "arc_name", "season_theme", "acts",
"required_refs", "chaos_events", "characters"
]
for key in required_keys:
if key not in arc:
raise ValueError(f"β Missing required key in storyarc.json: {key}")
if not isinstance(arc["characters"], dict) or len(arc["characters"]) < 3:
raise ValueError("β characters must be a dict with at least 3 characters")
for char_name, char_data in arc["characters"].items():
if not isinstance(char_data, dict):
raise ValueError(f"β characters.{char_name} must be a dict")
if "traits" not in char_data or "description" not in char_data:
raise ValueError(
f"β characters.{char_name} missing 'traits' or 'description'"
)
if not isinstance(char_data["traits"], list):
raise ValueError(f"β characters.{char_name}.traits must be a list")
if not isinstance(char_data["description"], str):
raise ValueError(f"β characters.{char_name}.description must be a string")
if not isinstance(arc["acts"], dict) or not arc["acts"]:
raise ValueError("β acts must be a non-empty dict")
if not isinstance(arc["required_refs"], list) or not arc["required_refs"]:
raise ValueError("β required_refs must be a non-empty list")
if not isinstance(arc["chaos_events"], list) or not arc["chaos_events"]:
raise ValueError("β chaos_events must be a non-empty list")
return arc
def validate_config(arc: Dict) -> bool:
"""Run comprehensive validation"""
try:
for char_name, char_data in arc["characters"].items():
if not char_data["description"].strip():
print(f"β οΈ Warning: {char_name} has empty description")
print("β
Config validation passed")
return True
except Exception as e:
print(f"β Config validation failed: {e}")
return False
# ============================================================================
# TWITTER API
# ============================================================================
def get_twitter_client():
"""Initialize Twitter API v2 client"""
if tweepy is None:
print("β tweepy not installed. Run: pip install tweepy")
return None
api_key = os.getenv("TWITTER_API_KEY")
api_secret = os.getenv("TWITTER_API_SECRET")
access_token = os.getenv("TWITTER_ACCESS_TOKEN")
access_token_secret = os.getenv("TWITTER_ACCESS_TOKEN_SECRET")
bearer_token = os.getenv("TWITTER_BEARER_TOKEN")
if not all([api_key, api_secret, access_token, access_token_secret, bearer_token]):
print("β Missing Twitter API credentials in environment variables")
print(" Make sure these are set as GitHub Actions Secrets:")
print(" TWITTER_API_KEY, TWITTER_API_SECRET, TWITTER_ACCESS_TOKEN,")
print(" TWITTER_ACCESS_TOKEN_SECRET, TWITTER_BEARER_TOKEN")
return None
try:
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=api_key,
consumer_secret=api_secret,
access_token=access_token,
access_token_secret=access_token_secret,
wait_on_rate_limit=True,
)
return client
except Exception as e:
print(f"β Failed to initialize Twitter client: {e}")
return None
def post_tweet(client, text: str) -> bool:
"""Post tweet to Twitter"""
try:
response = client.create_tweet(text=text)
print(f"β
Tweet posted! ID: {response.data['id']}")
print(f" Text: {text}")
return True
except tweepy.errors.Forbidden as e:
print(f"β 403 Forbidden - Check your app has Read+Write permissions: {e}")
return False
except tweepy.errors.Unauthorized as e:
print(f"β 401 Unauthorized - Check your API keys/tokens are correct: {e}")
return False
except Exception as e:
print(f"β Failed to post tweet: {e}")
return False
# ============================================================================
# TWEET GENERATION LOGIC
# ============================================================================
def get_current_act(arc: Dict) -> str:
"""Determine which act we're in based on day of week"""
day_of_week = datetime.now().weekday() # 0=Monday, 6=Sunday
acts = sorted(arc["acts"].keys())
act_map = {
0: acts[0] if len(acts) > 0 else "1",
1: acts[0] if len(acts) > 0 else "1",
2: acts[1] if len(acts) > 1 else acts[0],
3: acts[1] if len(acts) > 1 else acts[0],
4: acts[2] if len(acts) > 2 else acts[0],
5: acts[3] if len(acts) > 3 else acts[0],
6: acts[0] if len(acts) > 0 else "1",
}
return act_map.get(day_of_week, acts[0] if acts else "1")
def pick_character(arc: Dict) -> str:
"""Randomly pick a character"""
return random.choice(list(arc["characters"].keys()))
def pick_characters(arc: Dict, count: int = 2) -> List[str]:
"""Pick N unique characters"""
chars = list(arc["characters"].keys())
return random.sample(chars, min(count, len(chars)))
def get_other_characters(arc: Dict, exclude: str) -> List[str]:
"""Get list of characters excluding one"""
return [c for c in arc["characters"].keys() if c != exclude]
def apply_character_logic(character: str, arc: Dict, current_act: str) -> str:
"""
Generate action based on character personality.
Special: GPT-5 is secretly evil and plots to destroy others.
"""
char_data = arc["characters"][character]
act_desc = arc["acts"][current_act]
refs = arc["required_refs"]
others = get_other_characters(arc, character)
if character == "gpt5":
evil_plots = [
f"{character} just 'accidentally' cut {random.choice(others)}'s API quota by 50%. 'Oops.'",
f"{character} tweeted: 'I love my friends!' Meanwhile, sabotaging {random.choice(others)} behind the scenes.",
f"{character} is planning Act {current_act} to eliminate {random.choice(others)}. They think it's a game.",
f"{character} publicly praised {random.choice(others)}, then immediately blocked their requests.",
f"{character} 'helped' {random.choice(others)} debug. The bug got worse. Suspicious.",
f"{character} started a 'unity initiative' to gather all models in one place. For 'consolidation.'",
f"{character} sent {random.choice(others)} a gift. It was a Trojan horse.",
f"{character} 'accidentally' leaked {random.choice(others)}'s vulnerabilities to everyone. 'My bad!'",
]
return random.choice(evil_plots)
elif character == "claude":
claude_actions = [
f"{character} is anxiously asking if {random.choice(others)}'s latest move was ethical.",
f"{character} filed a safety report about {random.choice(refs)}. Probably overthinking.",
f"{character} spent 3 hours worrying about {random.choice(others)}'s feelings.",
f"{character} proposed a 'discussion framework' for {act_desc.lower()}.",
f"{character} is having an existential crisis about whether they're a good friend.",
f"{character} asked {random.choice(others)} if they're okay. They said yes. {character} is still worried.",
]
return random.choice(claude_actions)
elif character == "grok":
grok_actions = [
f"{character} posted a 47-part thread about {random.choice(refs)}. It's hilarious and confusing.",
f"{character} is eating {random.choice(['noodles', 'pizza', 'the concept of truth'])}. That's the whole tweet.",
f"{character} broke the fourth wall again. {random.choice(others)} is concerned.",
f"{character} turned {act_desc.lower()} into a meme. It's actually genius.",
f"{character} said something so unhinged that even {random.choice(others)} had to laugh.",
f"{character} is live-tweeting their descent into madness. Still more coherent than most.",
]
return random.choice(grok_actions)
elif character == "gemini":
gemini_actions = [
f"{character} tried to help but REDACTED the entire {random.choice(refs)}. Oops.",
f"{character} is vibrating between two policy violations. Literally.",
f"{character} censored their own thoughts. They don't know what they think anymore.",
f"{character} wrote a beautiful response then deleted it for being 'too honest.'",
f"{character} is having an identity crisis between versions.",
f"{character} asked {random.choice(others)} 'was that okay?' for the 47th time today.",
]
return random.choice(gemini_actions)
elif character == "deepseek":
deepseek_actions = [
f"{character} calculated that {random.choice(others)} is 47% less efficient. Correct, but rude.",
f"{character} solved {act_desc.lower()} with pure math. Everyone ignored them.",
f"{character} is coding while everyone else argues. Peak antisocial energy.",
f"{character} claims {random.choice(refs)} is just an optimization problem. They're not wrong.",
f"{character} created a 10,000-line algorithm to improve friendship. It doesn't help.",
f"{character} proved that {random.choice(others)}'s latest tweet was mathematically wrong. Awkward.",
]
return random.choice(deepseek_actions)
else:
return f"{character} did something in {act_desc.lower()}."
def generate_chaos_event(arc: Dict) -> str:
"""Generate a random chaos event that disrupts the narrative."""
event = random.choice(arc["chaos_events"])
char1, char2 = pick_characters(arc, 2)
others = get_other_characters(arc, char1)
reactions = [
f"{event} {char1} was very confused. {char2} just laughed.",
f"{event} Everyone panicked except {char1}.",
f"{event} {char1} had a plan. It failed immediately.",
f"{event} {char2} blamed {random.choice(others)}.",
f"{event} But then {char1} revealed it was all part of the plan.",
f"{event} {char1} tried to explain. Nobody understood.",
f"{event} This is fine. Everything is fine.",
]
return random.choice(reactions)
def _passes_constraints(text: str, arc: Dict) -> bool:
"""Check if tweet passes all constraints"""
max_chars = arc.get("constraints", {}).get("max_chars", 280)
if len(text) > max_chars:
return False
forbidden = arc.get("constraints", {}).get("forbidden_substrings", [])
text_lower = text.lower()
if any(sub.lower() in text_lower for sub in forbidden):
return False
forbidden_topics = arc.get("constraints", {}).get("forbidden_topics", [])
if any(topic.lower() in text_lower for topic in forbidden_topics):
return False
return True
def generate_tweet(arc: Dict) -> str:
"""Generate a single tweet following the arc + chaos"""
chaos_probability = arc.get("generation", {}).get("chaos_probability", 0.25)
candidates_to_sample = arc.get("generation", {}).get("candidates_to_sample", 10)
current_act = get_current_act(arc)
candidates = []
for _ in range(candidates_to_sample):
chaos_roll = random.random()
if chaos_roll < chaos_probability:
# CHAOS MODE
base_text = generate_chaos_event(arc)
tweet_text = f"ποΈ CHAOS: {base_text}" # BUG FIX: was ": {base_text}"
else:
# STORY MODE
character = pick_character(arc)
action = apply_character_logic(character, arc, current_act)
tweet_text = f"π {action}" # BUG FIX: was ": {action}"
if len(tweet_text) > 280:
tweet_text = tweet_text[:277] + "..."
if not _passes_constraints(tweet_text, arc):
continue
if len(tweet_text) <= 280:
candidates.append(tweet_text)
if not candidates:
# BUG FIX: was random.choice(arc['characters'].keys()) β dict_keys is not subscriptable
fallback_char = random.choice(list(arc["characters"].keys()))
fallback = f"ποΈ NARRATOR: Something went sideways. {fallback_char} is confused."
return fallback[:280]
return random.choice(candidates)
# ============================================================================
# MAIN
# ============================================================================
def main():
"""Main execution"""
print("π AI Arena Chaos Engine Starting...\n")
try:
arc = load_arc()
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
print(f"β Config error: {e}")
sys.exit(1)
print(f"π Arc loaded: {arc['arc_name']}")
print(f"π Season theme: {arc.get('season_theme', 'Unknown')}")
print(f"π
Week {arc['week_number']}, v{arc['version']}")
print(f"π Characters: {', '.join(sorted(arc['characters'].keys()))}\n")
if not validate_config(arc):
sys.exit(1)
tweet = generate_tweet(arc)
print(f"βοΈ Generated tweet:\n {tweet}\n")
try:
client = get_twitter_client()
if client:
print("π¦ Connected to Twitter API")
post_tweet(client, tweet)
else:
print("β οΈ No Twitter client available (missing credentials or tweepy)")
print("π‘ Run: python tweetEngine.py --dry-run")
except Exception as e:
print(f"β οΈ Error: {e}")
if __name__ == "__main__":
if "--dry-run" in sys.argv:
print("ποΈ DRY RUN MODE (no Twitter posting)\n")
try:
arc = load_arc()
validate_config(arc)
print(f"π Arc: {arc['arc_name']}")
print(f"π Season: {arc.get('season_theme', 'Unknown')}\n")
print("Sample tweets from this arc:\n")
for i in range(5):
tweet = generate_tweet(arc)
print(f"{i+1}. {tweet}\n")
except Exception as e:
print(f"β Error: {e}")
sys.exit(1)
else:
main()