-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmain.py
More file actions
3940 lines (3324 loc) · 179 KB
/
Copy pathmain.py
File metadata and controls
3940 lines (3324 loc) · 179 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# main.py
# Suppress Pydantic warnings about "model_" field names conflicting with protected namespaces.
# These warnings come from third-party API SDKs (OpenAI, etc.) and don't affect our code.
# The warnings are harmless but clutter the console output on startup.
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
import os
import time
import threading
import json
import sys
import re
from dotenv import load_dotenv
from PyQt6.QtWidgets import QApplication, QMessageBox
from PyQt6.QtCore import QThread, pyqtSignal, QObject, QRunnable, pyqtSlot, QThreadPool, QTimer
from datetime import datetime
import requests
# Load environment variables from .env file
load_dotenv()
from config import (
TURN_DELAY,
AI_MODELS,
SYSTEM_PROMPT_PAIRS,
SHOW_CHAIN_OF_THOUGHT_IN_CONTEXT,
SHARE_CHAIN_OF_THOUGHT,
DEVELOPER_TOOLS,
get_model_tier_by_id,
get_display_name
)
from shared_utils import (
call_claude_api,
call_openrouter_api,
call_openai_api,
call_replicate_api,
call_deepseek_api,
open_html_in_browser,
generate_image_from_text,
generate_video_with_sora
)
from gui import LiminalBackroomsApp, load_fonts
from command_parser import parse_commands, AgentCommand, format_command_result
# Import freeze detector for debugging (only used when DEVELOPER_TOOLS is enabled)
if DEVELOPER_TOOLS:
try:
from tools.freeze_detector import FreezeDetector, enable_faulthandler
_FREEZE_DETECTOR_AVAILABLE = True
except ImportError as e:
print(f"Warning: Could not load freeze detector: {e}")
_FREEZE_DETECTOR_AVAILABLE = False
else:
_FREEZE_DETECTOR_AVAILABLE = False
# =============================================================================
# LOGS DIRECTORY SETUP
# =============================================================================
# All log files (crash_log.txt, freeze_log.txt) go here
# This folder should be gitignored
LOGS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
os.makedirs(LOGS_DIR, exist_ok=True)
def is_image_message(message: dict) -> bool:
"""Returns True if 'message' contains a base64 image in its 'content' list."""
if not isinstance(message, dict):
return False
content = message.get('content', [])
if isinstance(content, list):
for part in content:
if part.get('type') == 'image':
return True
return False
class WorkerSignals(QObject):
"""Defines the signals available from a running worker thread"""
finished = pyqtSignal()
error = pyqtSignal(str)
response = pyqtSignal(str, str)
result = pyqtSignal(str, object) # Signal for complete result object
progress = pyqtSignal(str)
streaming_chunk = pyqtSignal(str, str) # Signal for streaming tokens: (ai_name, chunk)
started = pyqtSignal(str, str) # Signal when AI starts processing: (ai_name, model)
class ImageUpdateSignals(QObject):
"""Signals for updating UI with generated images from background threads"""
image_ready = pyqtSignal(dict, str) # (image_message, image_path)
image_failed = pyqtSignal(str, str, str) # (ai_name, prompt, error_message)
class VideoUpdateSignals(QObject):
"""Signals for updating UI with generated videos from background threads"""
video_ready = pyqtSignal(str, str, str, str) # (video_path, prompt, ai_name, model)
video_failed = pyqtSignal(str, str, str) # (ai_name, prompt, error_message)
class Worker(QRunnable):
"""Worker thread for processing AI turns using QThreadPool"""
def __init__(self, ai_name, conversation, model, system_prompt, is_branch=False, branch_id=None, gui=None, invite_tier="Both", prompt_modifications=None, ai_temperatures=None):
super().__init__()
self.ai_name = ai_name
self.conversation = conversation.copy() # Make a copy to prevent race conditions
self.model = model
self.system_prompt = system_prompt
self.is_branch = is_branch
self.branch_id = branch_id
self.gui = gui
self.invite_tier = invite_tier
self.prompt_modifications = prompt_modifications or {}
self.ai_temperatures = ai_temperatures or {}
# Create signals object
self.signals = WorkerSignals()
@pyqtSlot()
def run(self):
"""Process the AI turn when the thread is started"""
print(f"[Worker] >>> Starting run() for {self.ai_name} ({self.model})")
# Emit started signal so UI can show typing indicator
self.signals.started.emit(self.ai_name, self.model)
try:
# Emit progress update
self.signals.progress.emit(f"Processing {self.ai_name} turn with {self.model}...")
# Define streaming callback
def stream_chunk(chunk: str):
self.signals.streaming_chunk.emit(self.ai_name, chunk)
# Process the turn with streaming
print(f"[Worker] Calling ai_turn for {self.ai_name}...")
result = ai_turn(
self.ai_name,
self.conversation,
self.model,
self.system_prompt,
gui=self.gui,
streaming_callback=stream_chunk,
invite_tier=self.invite_tier,
prompt_modifications=self.prompt_modifications,
ai_temperatures=self.ai_temperatures
)
print(f"[Worker] ai_turn completed for {self.ai_name}, result type: {type(result)}")
# Emit both the text response and the full result object
if isinstance(result, dict):
response_content = result.get('content', '')
print(f"[Worker] Emitting response for {self.ai_name}, content length: {len(response_content) if response_content else 0}")
# Emit the simple text response for backward compatibility
self.signals.response.emit(self.ai_name, response_content)
# Also emit the full result object for HTML contribution processing
self.signals.result.emit(self.ai_name, result)
else:
# Handle simple string responses
print(f"[Worker] Emitting string response for {self.ai_name}")
self.signals.response.emit(self.ai_name, result if result else "")
self.signals.result.emit(self.ai_name, {"content": result, "model": self.model})
# Emit finished signal
print(f"[Worker] <<< Finished run() for {self.ai_name}, emitting finished signal")
self.signals.finished.emit()
except Exception as e:
# Emit error signal
print(f"[Worker] !!! ERROR in run() for {self.ai_name}: {e}")
import traceback
traceback.print_exc()
self.signals.error.emit(str(e))
# Still emit finished signal even if there's an error
self.signals.finished.emit()
def ai_turn(ai_name, conversation, model, system_prompt, gui=None, is_branch=False, branch_output=None, streaming_callback=None, invite_tier="Both", prompt_modifications=None, ai_temperatures=None):
"""Execute an AI turn with the given parameters
Args:
streaming_callback: Optional function(chunk: str) to call with each streaming token
invite_tier: "Free", "Paid", or "Both" - controls which models AI can invite
prompt_modifications: Optional dict mapping AI names to custom system prompts
ai_temperatures: Optional dict mapping AI names to temperature values
"""
print(f"==================================================")
print(f"Starting {model} turn ({ai_name})...")
print(f"Current conversation length: {len(conversation)}")
# HTML contributions and living document disabled
enhanced_system_prompt = system_prompt
# The model parameter is now the actual model ID (from get_selected_model_id)
model_id = model
# Inject available models based on invite tier setting
from config import get_invite_models_text
models_text = get_invite_models_text(invite_tier)
# Debug: log the tier setting and models text
print(f"[AI Turn] Tier setting: {invite_tier}")
print(f"[AI Turn] Models text: {models_text}")
# Replace placeholder in prompt if exists, otherwise append
if "!add_ai" in enhanced_system_prompt:
# Find and update model list placeholder or existing model list
import re
# Match the placeholder text: [Models list injected based on tier setting]
placeholder_pattern = r'\[Models list injected based on tier setting\]'
# Match our emphatic format: ⚠️ ONLY USE FREE/PAID MODELS: ... — DO NOT ...
emphatic_pattern = r'⚠️ ONLY USE (?:FREE|PAID) MODELS:[^\n]*'
# Also match old format or "Available models" line
legacy_pattern = r'(?:FREE MODELS[^:]*:|PAID MODELS[^:]*:|Available[^:]*:)[^\n]*'
if re.search(placeholder_pattern, enhanced_system_prompt):
# Replace the placeholder with actual model list
enhanced_system_prompt = re.sub(placeholder_pattern, models_text, enhanced_system_prompt)
print(f"[AI Turn] Replaced placeholder with models list")
elif re.search(emphatic_pattern, enhanced_system_prompt):
# Replace existing emphatic model list line (from previous turn)
enhanced_system_prompt = re.sub(emphatic_pattern, models_text, enhanced_system_prompt)
print(f"[AI Turn] Replaced emphatic models line")
elif re.search(legacy_pattern, enhanced_system_prompt):
# Replace legacy model list line
enhanced_system_prompt = re.sub(legacy_pattern, models_text, enhanced_system_prompt)
print(f"[AI Turn] Replaced legacy models line")
else:
# Add after !add_ai line if no placeholder found
enhanced_system_prompt = enhanced_system_prompt.replace(
'!add_ai "Model Name"',
f'!add_ai "Model Name"\n {models_text}\n '
)
print(f"[AI Turn] Appended models list after !add_ai")
# Prepend model identity to system prompt so AI knows who it is
display_name = get_display_name(model_id)
enhanced_system_prompt = f"You are {ai_name} ({display_name}).\n\n{enhanced_system_prompt}"
# Check for branch type and count AI responses
is_rabbithole = False
is_fork = False
branch_text = ""
ai_response_count = 0
found_branch_marker = False
latest_branch_marker_index = -1
# First find the most recent branch marker
for i, msg in enumerate(conversation):
if isinstance(msg, dict) and msg.get("_type") == "branch_indicator":
latest_branch_marker_index = i
found_branch_marker = True
# Determine branch type from the latest marker
msg_content = msg.get("content", "")
# Branch indicators are always plain strings
if isinstance(msg_content, str):
if "Rabbitholing down:" in msg_content:
is_rabbithole = True
branch_text = msg_content.split('"')[1] if '"' in msg_content else ""
print(f"Detected rabbithole branch for: '{branch_text}'")
elif "Forking off:" in msg_content:
is_fork = True
branch_text = msg_content.split('"')[1] if '"' in msg_content else ""
print(f"Detected fork branch for: '{branch_text}'")
# Now count AI responses that occur AFTER the latest branch marker
ai_response_count = 0
if found_branch_marker:
for i, msg in enumerate(conversation):
if i > latest_branch_marker_index and msg.get("role") == "assistant":
ai_response_count += 1
print(f"Counting AI responses after latest branch marker: found {ai_response_count} responses")
# Handle branch-specific system prompts
# For rabbitholing: override system prompt for first TWO responses
if is_rabbithole and ai_response_count < 2:
print(f"USING RABBITHOLE PROMPT: '{branch_text}' - response #{ai_response_count+1} after branch")
system_prompt = f"'{branch_text}'!!!"
# For forking: override system prompt ONLY for first response
elif is_fork and ai_response_count == 0:
print(f"USING FORK PROMPT: '{branch_text}' - response #{ai_response_count+1}")
system_prompt = f"The conversation forks from'{branch_text}'. Continue naturally from this point."
# For all other cases, use the standard system prompt
else:
if is_rabbithole:
print(f"USING STANDARD PROMPT: Past initial rabbithole exploration (responses after branch: {ai_response_count})")
elif is_fork:
print(f"USING STANDARD PROMPT: Past initial fork response (responses after branch: {ai_response_count})")
# Apply the enhanced system prompt (with HTML contribution instructions)
system_prompt = enhanced_system_prompt
# Check if this AI has added to their prompt via !prompt command
if prompt_modifications and ai_name in prompt_modifications:
# Note: This is for compatibility - the variable name is prompt_modifications
# but it's actually used as prompt_additions (list-based)
if isinstance(prompt_modifications, dict) and ai_name in prompt_modifications:
if isinstance(prompt_modifications[ai_name], list):
# List-based additions (upstream approach)
additions = prompt_modifications[ai_name]
if additions:
formatted_additions = "\n\n[Your remembered insights/perspectives]:\n- " + "\n- ".join(additions)
system_prompt += formatted_additions
print(f"[AI Turn] Applied {len(additions)} prompt additions for {ai_name}")
else:
# Single string (fallback for old approach)
custom_prompt = prompt_modifications[ai_name]
print(f"[AI Turn] Using custom prompt for {ai_name}: {custom_prompt[:100]}...")
system_prompt = custom_prompt
# Get temperature for this AI (default 1.0)
temperature = 1.0
if ai_temperatures and ai_name in ai_temperatures:
temperature = ai_temperatures[ai_name]
print(f"[AI Turn] Using custom temperature for {ai_name}: {temperature}")
# CRITICAL: Always ensure we have the system prompt
# No matter what happens with the conversation, we need this
messages = []
messages.append({
"role": "system",
"content": system_prompt
})
# Filter out any existing system messages that might interfere
filtered_conversation = []
for msg in conversation:
if not isinstance(msg, dict):
# Convert plain text to dictionary
msg = {"role": "user", "content": str(msg)}
# Skip any hidden "connecting..." messages
msg_content = msg.get("content", "")
if msg.get("hidden") and isinstance(msg_content, str) and "connect" in msg_content.lower():
continue
# Skip empty messages
content = msg.get("content", "")
if isinstance(content, str):
if not content.strip():
continue
elif isinstance(content, list):
# For structured content, skip if all parts are empty
if not any(part.get('text', '').strip() if part.get('type') == 'text' else True for part in content):
continue
else:
if not content:
continue
# Skip system messages (we already added our own above)
if msg.get("role") == "system":
continue
# Skip special system messages (branch indicators, etc.)
if msg.get("role") == "system" and msg.get("_type"):
continue
# Skip duplicate messages - check if this exact content exists already
is_duplicate = False
for existing in filtered_conversation:
if existing.get("content") == msg.get("content"):
is_duplicate = True
content = msg.get('content', '')
# Safely preview content - handle both string and list (structured) content
if isinstance(content, str):
preview = content[:30] + "..." if len(content) > 30 else content
else:
preview = f"[structured content with {len(content)} parts]"
print(f"Skipping duplicate message: {preview}")
break
if not is_duplicate:
filtered_conversation.append(msg)
# Filter whisper messages - only include whispers addressed to this AI
whisper_filtered = []
for msg in filtered_conversation:
if msg.get('_type') == 'whisper':
# Only include whispers addressed to this AI
if msg.get('_whisper_to', '').upper() == ai_name.upper():
whisper_filtered.append(msg)
# Skip whispers for other AIs
else:
whisper_filtered.append(msg)
filtered_conversation = whisper_filtered
# Process filtered conversation
for i, msg in enumerate(filtered_conversation):
# Check if this message is from the current AI
is_from_this_ai = False
if msg.get("ai_name") == ai_name:
is_from_this_ai = True
# Determine role
if is_from_this_ai:
role = "assistant"
else:
role = "user"
# Get content - preserve structure for images
content = msg.get("content", "")
# Inject speaker name for messages from other participants (not from current AI)
if not is_from_this_ai and content:
# Use the model name (e.g., "Claude 4.5 Sonnet") if available, otherwise fall back to ai_name or user's name
speaker_name = msg.get("model") or msg.get("ai_name") or msg.get("_user_name", "User")
# Handle different content types
if isinstance(content, str):
# Simple string content - prefix with speaker name
content = f"[{speaker_name}]: {content}"
elif isinstance(content, list):
# Structured content (e.g., with images) - prefix text parts
modified_content = []
for part in content:
if part.get('type') == 'text':
# Prefix the first text part with speaker name
text = part.get('text', '')
modified_part = part.copy()
modified_part['text'] = f"[{speaker_name}]: {text}"
modified_content.append(modified_part)
# Only prefix the first text part
break
else:
modified_content.append(part)
# Add remaining parts unchanged
first_text_found = False
for part in content:
if part.get('type') == 'text' and not first_text_found:
first_text_found = True
continue # Skip, already added above
modified_content.append(part)
content = modified_content if modified_content else content
# Add to messages
messages.append({
"role": role,
"content": content # Now includes speaker names for non-current-AI messages
})
# For logging, handle both string and structured content
if isinstance(content, list):
print(f"Message {i} - AI: {msg.get('ai_name', 'User')} - Assigned role: {role} - Content: [structured message with {len(content)} parts]")
else:
content_preview = content[:50] + "..." if len(str(content)) > 50 else content
print(f"Message {i} - AI: {msg.get('ai_name', 'User')} - Assigned role: {role} - Preview: {content_preview}")
# Ensure the last message is a user message so the AI responds
if len(messages) > 1 and messages[-1].get("role") == "assistant":
# Find an appropriate message to use
if is_rabbithole and branch_text:
# Add a special rabbitholing instruction as the last message
messages.append({
"role": "user",
"content": f"Please explore the concept of '{branch_text}' in depth. What are the most interesting aspects or connections related to this concept?"
})
elif is_fork and branch_text:
# Add a special forking instruction as the last message
messages.append({
"role": "user",
"content": f"Continue on naturally from the point about '{branch_text}' without including this text."
})
else:
# Standard handling for other conversations
# Find the most recent message from the other AI to use as prompt
other_ai_message = None
for msg in reversed(filtered_conversation):
if msg.get("ai_name") != ai_name:
other_ai_message = msg.get("content", "")
break
if other_ai_message:
messages.append({
"role": "user",
"content": other_ai_message
})
else:
# Fallback - only if no other AI message found
messages.append({
"role": "user",
"content": "Let's continue our conversation."
})
# Print the processed messages for debugging
print(f"Sending to {model} ({ai_name}):")
for i, msg in enumerate(messages):
role = msg.get("role", "unknown")
content_raw = msg.get("content", "")
# Handle both string and list content for logging
if isinstance(content_raw, list):
text_parts = [part.get('text', '') for part in content_raw if part.get('type') == 'text']
has_image = any(part.get('type') == 'image' for part in content_raw)
content_str = ' '.join(text_parts)
if has_image:
content_str = f"[Image] {content_str}" if content_str else "[Image]"
else:
content_str = str(content_raw)
# Truncate for display
content = content_str[:50] + "..." if len(content_str) > 50 else content_str
print(f"[{i}] {role}: {content}")
# Load any available memories for this AI
memories = []
try:
if os.path.exists(f'memories/{ai_name.lower()}_memories.json'):
with open(f'memories/{ai_name.lower()}_memories.json', 'r') as f:
memories = json.load(f)
print(f"Loaded {len(memories)} memories for {ai_name}")
else:
print(f"Loaded 0 memories for {ai_name}")
except Exception as e:
print(f"Error loading memories: {e}")
print(f"Loaded 0 memories for {ai_name}")
# Display the final processed messages for debugging (avoid printing base64 images)
print(f"Sending to Claude:")
print(f"Messages: {len(messages)} message(s)")
# Display the prompt
print(f"--- Prompt to {model} ({ai_name}) ---")
try:
# Route Sora video models
if model_id in ("sora-2", "sora-2-pro"):
print(f"Using Sora Video API for model: {model_id}")
# Use last user message as the video prompt
prompt_content = ""
if len(messages) > 0:
last_content = messages[-1].get("content", "")
# Extract text from structured content if needed
if isinstance(last_content, list):
text_parts = [part.get('text', '') for part in last_content if part.get('type') == 'text']
prompt_content = ' '.join(text_parts)
elif isinstance(last_content, str):
prompt_content = last_content
if not prompt_content or not prompt_content.strip():
prompt_content = "A short abstract motion graphic in warm colors"
# Use config values with env var override
from config import SORA_SECONDS, SORA_SIZE
sora_seconds = int(os.getenv("SORA_SECONDS", str(SORA_SECONDS)))
sora_size = os.getenv("SORA_SIZE", SORA_SIZE) or None
print(f"[Sora] Starting job with seconds={sora_seconds} size={sora_size}")
video_result = generate_video_with_sora(
prompt=prompt_content,
model=model_id,
seconds=sora_seconds,
size=sora_size,
)
if video_result.get("success"):
print(f"[Sora] Completed: id={video_result.get('video_id')} path={video_result.get('video_path')}")
# Return a lightweight textual confirmation; video is saved to disk
return {
"role": "assistant",
"content": f"[Sora] Video created: {video_result.get('video_path')}",
"model": model,
"ai_name": ai_name
}
else:
err = video_result.get("error", "unknown error")
print(f"[Sora] Failed: {err}")
return {
"role": "system",
"content": f"[Sora] Video generation failed: {err}",
"model": model,
"ai_name": ai_name
}
# Route Claude models through OpenRouter instead of direct Anthropic API
# This avoids issues with image handling differences between the APIs
# Set to False to use OpenRouter for Claude (recommended for image support)
USE_DIRECT_ANTHROPIC_API = False
if USE_DIRECT_ANTHROPIC_API and ("claude" in model_id.lower() or model_id in ["anthropic/claude-3-opus-20240229", "anthropic/claude-3-sonnet-20240229", "anthropic/claude-3-haiku-20240307"]):
print(f"Using Claude API for model: {model_id}")
# CRITICAL: Make sure there are no duplicates in the messages and system prompt is included
final_messages = []
seen_contents = set()
for msg in messages:
# Skip empty messages - handle both string and list content
content = msg.get("content", "")
is_empty = False
if isinstance(content, list):
# For structured content, check if all parts are empty
text_parts = [part.get('text', '').strip() for part in content if part.get('type') == 'text']
has_image = any(part.get('type') == 'image' for part in content)
is_empty = not text_parts and not has_image
elif isinstance(content, str):
is_empty = not content
else:
is_empty = not content
if is_empty:
continue
# Handle system message separately
if msg.get("role") == "system":
continue
# Check for duplicates by content - create hashable representation
content = msg.get("content", "")
# Create a hashable content_hash for duplicate detection
if isinstance(content, list):
# For structured messages, use text parts for hash
text_parts = [part.get('text', '') for part in content if part.get('type') == 'text']
content_hash = ''.join(text_parts)
elif isinstance(content, str):
content_hash = content
else:
content_hash = str(content) if content else ""
if content_hash and content_hash in seen_contents:
print(f"Skipping duplicate message in AI turn: {content_hash[:30]}...")
continue
if content_hash:
seen_contents.add(content_hash)
final_messages.append(msg)
# Ensure we have at least one message
if not final_messages:
print("Warning: No messages left after filtering. Adding a default message.")
final_messages.append({"role": "user", "content": "Connecting..."})
# Get the prompt content safely
prompt_content = ""
if len(final_messages) > 0:
prompt_content = final_messages[-1].get("content", "")
# Use all messages except the last one as context
context_messages = final_messages[:-1]
else:
context_messages = []
prompt_content = "Connecting..." # Default fallback
# Call Claude API with filtered messages (with streaming if callback provided)
response = call_claude_api(prompt_content, context_messages, model_id, system_prompt, stream_callback=streaming_callback)
return {
"role": "assistant",
"content": response,
"model": model,
"ai_name": ai_name
}
# Check for DeepSeek models to use Replicate via DeepSeek API function
if "deepseek" in model.lower():
print(f"Using Replicate API for DeepSeek model: {model_id}")
# Ensure we have at least one message for the prompt
if len(messages) > 0:
prompt_content = messages[-1].get("content", "")
context_messages = messages[:-1]
else:
prompt_content = "Connecting..."
context_messages = []
response = call_deepseek_api(prompt_content, context_messages, model_id, system_prompt)
# Ensure response has the required format for the Worker class
if isinstance(response, dict) and 'content' in response:
# Add model info to the response
response['model'] = model
response['role'] = 'assistant'
response['ai_name'] = ai_name
# Check for HTML contribution
if "html_contribution" in response:
html_contribution = response["html_contribution"]
# Don't update HTML document here - we'll do it in on_ai_result_received
# Just add indicator to the conversation part
response["content"] += "\n\n..."
if "display" in response:
response["display"] += "\n\n..."
return response
else:
# Create a formatted response if not already in the right format
return {
"role": "assistant",
"content": str(response) if response else "No response from model",
"model": model,
"ai_name": ai_name,
"display": str(response) if response else "No response from model"
}
# Use OpenRouter for all other models
else:
print(f"Using OpenRouter API for model: {model_id}")
try:
# Ensure we have valid messages
if len(messages) > 0:
prompt_content = messages[-1].get("content", "")
context_messages = messages[:-1]
else:
prompt_content = "Connecting..."
context_messages = []
# Call OpenRouter API with streaming support
response = call_openrouter_api(prompt_content, context_messages, model_id, system_prompt, stream_callback=streaming_callback, temperature=temperature)
# Avoid printing full response which could be large
response_preview = str(response)[:200] + "..." if response and len(str(response)) > 200 else response
print(f"Raw {model} Response: {response_preview}")
result = {
"role": "assistant",
"content": response,
"model": model,
"ai_name": ai_name
}
return result
except Exception as e:
error_message = f"Error making API request: {str(e)}"
print(f"Error: {error_message}")
print(f"Error type: {type(e)}")
# Create an error response
result = {
"role": "system",
"content": f"Error: {error_message}",
"model": model,
"ai_name": ai_name
}
# Return the error result
return result
except Exception as e:
error_message = f"Error making API request: {str(e)}"
print(f"Error: {error_message}")
# Create an error response
result = {
"role": "system",
"content": f"Error: {error_message}",
"model": model,
"ai_name": ai_name
}
# Return the error result
return result
class ConversationManager:
"""Manages conversation processing and state"""
def __init__(self, app):
self.app = app
self.workers = [] # Keep track of worker threads
# Initialize AI command state dictionaries
self.ai_prompt_additions = {} # Store prompt additions from !prompt command (list per AI)
self.ai_temperatures = {} # Store custom temperatures from !temperature command
# Initialize the worker thread pool
self.thread_pool = QThreadPool()
print(f"Conversation Manager initialized with {self.thread_pool.maxThreadCount()} threads")
# Set up image update signals for thread-safe UI updates
self.image_signals = ImageUpdateSignals()
self.image_signals.image_ready.connect(self._on_image_ready)
self.image_signals.image_failed.connect(self._on_image_failed)
# Set up video update signals for thread-safe UI updates
self.video_signals = VideoUpdateSignals()
self.video_signals.video_ready.connect(self._on_video_ready)
self.video_signals.video_failed.connect(self._on_video_failed)
# Step mode state - tracks which AI is next in single-turn mode
self.current_ai_index = 0 # 0-indexed: which AI's turn it is
self.round_in_progress = False # Whether we're mid-round in step mode
# Auto-save timer - save every 30 seconds
self._autosave_timer = QTimer()
self._autosave_timer.timeout.connect(self._auto_save_conversation)
self._autosave_timer.start(30000) # 30 seconds
from config import OUTPUTS_DIR
self._autosave_path = os.path.join(OUTPUTS_DIR, '.autosave_conversation.json')
def _auto_save_conversation(self):
"""Periodically auto-save conversation state for crash recovery."""
try:
if not hasattr(self.app, 'main_conversation') or not self.app.main_conversation:
return
# Only save if there's meaningful content (at least 2 messages)
if len(self.app.main_conversation) < 2:
return
# Build saveable data (strip non-serializable content)
save_data = {
'timestamp': datetime.now().isoformat(),
'conversation': []
}
for msg in self.app.main_conversation:
save_msg = {
'role': msg.get('role', ''),
'ai_name': msg.get('ai_name', ''),
'model': msg.get('model', ''),
'_type': msg.get('_type', ''),
}
# Handle content - skip binary image data
content = msg.get('content', '')
if isinstance(content, str):
save_msg['content'] = content
elif isinstance(content, list):
text_parts = []
for part in content:
if isinstance(part, dict) and part.get('type') == 'text':
text_parts.append(part.get('text', ''))
save_msg['content'] = '\n'.join(text_parts)
else:
save_msg['content'] = str(content)
# Skip empty/streaming messages
if msg.get('_streaming') or not save_msg['content'].strip():
continue
save_data['conversation'].append(save_msg)
# Write atomically (write to temp, then rename)
temp_path = self._autosave_path + '.tmp'
with open(temp_path, 'w', encoding='utf-8') as f:
json.dump(save_data, f, indent=2, ensure_ascii=False)
# Rename to final path (atomic on most systems)
import shutil
shutil.move(temp_path, self._autosave_path)
except Exception as e:
print(f"[AutoSave] Error: {e}")
def check_autosave_recovery(self):
"""Check for an auto-save file and offer to recover it."""
try:
if not os.path.exists(self._autosave_path):
return False
with open(self._autosave_path, 'r', encoding='utf-8') as f:
save_data = json.load(f)
timestamp = save_data.get('timestamp', 'unknown time')
msg_count = len(save_data.get('conversation', []))
if msg_count < 2:
# Not worth recovering
os.remove(self._autosave_path)
return False
# Ask user if they want to recover
from PyQt6.QtWidgets import QMessageBox
reply = QMessageBox.question(
self.app,
"Recover Previous Session?",
f"Found auto-saved conversation from {timestamp}\n"
f"({msg_count} messages)\n\n"
f"Would you like to recover it?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
# Restore conversation
self.app.main_conversation = save_data['conversation']
self.app.left_pane.conversation = self.app.main_conversation
self.app.left_pane.render_conversation()
self.app.statusBar().showMessage(f"Recovered {msg_count} messages from auto-save")
print(f"[AutoSave] Recovered {msg_count} messages from {timestamp}")
return True
else:
# User declined - remove the file
os.remove(self._autosave_path)
return False
except Exception as e:
print(f"[AutoSave] Recovery error: {e}")
return False
def clear_autosave(self):
"""Clear the auto-save file (e.g., after clean exit or reset)."""
try:
if os.path.exists(self._autosave_path):
os.remove(self._autosave_path)
except Exception:
pass
def _on_video_ready(self, video_path: str, prompt: str, ai_name: str, model: str):
"""Handle video ready signal - runs on main thread"""
try:
# Remove the "generating..." notification first
self._remove_pending_notification(ai_name, prompt)
# Format display name like message headings do
display_name = f"{ai_name} ({model})" if model else ai_name
print(f"[Agent] Video ready, updating UI: {video_path}")
# Update the video preview panel
if hasattr(self.app, 'right_sidebar') and hasattr(self.app.right_sidebar, 'update_video_preview'):
self.app.right_sidebar.update_video_preview(video_path, display_name, prompt)
# Update status bar notification with prompt (truncated for display)
if hasattr(self.app, 'notification_label'):
# Truncate long prompts for status bar
display_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
self.app.notification_label.setText(f"🎬 {display_name}: Video completed")
except Exception as e:
print(f"[Agent] Error handling video ready: {e}")
import traceback
traceback.print_exc()
def _on_video_failed(self, ai_name: str, prompt: str, error: str):
"""Handle video generation failure - runs on main thread"""
try:
# Remove the "generating..." notification first
self._remove_pending_notification(ai_name, prompt)
# Get AI's model name for consistent formatting
ai_num = int(ai_name.split('-')[1]) if '-' in ai_name else 1
model_name = self.get_model_for_ai(ai_num)
# Create a failure notification message that AIs can see
truncated_prompt = prompt[:50] + '...' if len(prompt) > 50 else prompt
# Parse and simplify error message for display, but log the full error
print(f"[Agent] ========== VIDEO GENERATION FAILED ==========")
print(f"[Agent] AI: {ai_name} ({model_name})")
print(f"[Agent] Prompt: {prompt[:100]}{'...' if len(prompt) > 100 else ''}")
print(f"[Agent] Full error: {error}")
print(f"[Agent] ==============================================")
# Determine user-friendly error message based on error content
error_lower = error.lower()
if "402" in error or "credits" in error_lower or "insufficient" in error_lower:
simple_error = "insufficient API credits"
detail = "Check your OpenAI balance (Sora requires credits)"
elif "429" in error or "rate" in error_lower or "limit" in error_lower:
simple_error = "rate limited"
detail = "Too many requests, please wait"
print(f"[Agent] >>> RATE LIMITED - Full response: {error}")
elif "401" in error or "unauthorized" in error_lower or "api key" in error_lower:
simple_error = "authentication failed"
detail = "Check your OPENAI_API_KEY"
elif "not set" in error_lower:
simple_error = "API key not configured"
detail = "Set OPENAI_API_KEY in .env file"
elif "timeout" in error_lower:
simple_error = "request timed out"
detail = "Video generation took too long"
elif "500" in error or "502" in error or "503" in error or "server" in error_lower:
simple_error = "server error"
detail = "OpenAI Sora is having issues"
elif "content" in error_lower and "policy" in error_lower:
simple_error = "content policy violation"
detail = "Prompt was rejected by safety filters"
elif "failed" in error_lower and "status" in error_lower:
simple_error = "video rendering failed"
detail = "Sora couldn't complete the video"
else:
simple_error = "generation failed"
# Extract a short error snippet if available
detail = error[:80] if len(error) <= 80 else error[:77] + "..."
print(f"[Agent] Simplified: {simple_error} — {detail}")
failure_message = {
"role": "system",
"content": f"❌ [{caller}]: !video \"{truncated_prompt}\" — {simple_error}",
"_type": "agent_notification",
"_command_success": False
}
# Add to conversation so AIs can see it
self.app.main_conversation.append(failure_message)
self.app.left_pane.conversation = self.app.main_conversation
self.app.left_pane.render_conversation()
# Update status bar with more detail
if hasattr(self.app, 'notification_label'):
self.app.notification_label.setText(f"❌ Video failed: {simple_error} — {detail}")
print(f"[Agent] Video failure notification added to conversation")
except Exception as e:
print(f"[Agent] Error handling video failure: {e}")
import traceback
traceback.print_exc()
def _on_image_failed(self, ai_name: str, prompt: str, error: str):