diff --git a/backend/routers/apps.py b/backend/routers/apps.py index 25879d034fa..3b8f87299eb 100644 --- a/backend/routers/apps.py +++ b/backend/routers/apps.py @@ -28,7 +28,7 @@ from utils.other import endpoints as auth from models.app import App, ActionType from utils.other.storage import upload_plugin_logo, delete_plugin_logo, upload_app_thumbnail, get_app_thumbnail_url -from utils.social import get_twitter_profile, get_twitter_timeline, verify_latest_tweet, \ +from utils.social import get_twitter_profile, verify_latest_tweet, \ upsert_persona_from_twitter_profile, add_twitter_to_persona router = APIRouter() diff --git a/backend/utils/apps.py b/backend/utils/apps.py index 501bede4e8e..cc035b764d7 100644 --- a/backend/utils/apps.py +++ b/backend/utils/apps.py @@ -79,7 +79,6 @@ def get_available_apps(uid: str, include_reviews: bool = False) -> List[App]: if cachedApps := get_generic_cache('get_public_approved_apps_data'): print('get_public_approved_plugins_data from cache----------------------------') public_approved_data = cachedApps - public_approved_data = get_public_approved_apps_db() public_unapproved_data = get_public_unapproved_apps(uid) private_data = get_private_apps(uid) pass @@ -394,7 +393,7 @@ async def generate_persona_prompt(uid: str, persona: dict): tweets = None if "twitter" in persona['connected_accounts']: - print("twitter in connected accounts---------------------------") + print("twitter is in connected accounts") # Get latest tweets tweets = await get_twitter_timeline(persona['twitter']['username']) tweets = [{'tweet': tweet['text'], 'posted_at': tweet['created_at']} for tweet in tweets['timeline']] @@ -474,13 +473,15 @@ def sync_update_persona_prompt(persona: dict): asyncio.set_event_loop(loop) try: return loop.run_until_complete(update_persona_prompt(persona)) + except Exception as e: + print(f"Error in update_persona_prompt for persona {persona.get('id', 'unknown')}: {str(e)}") + return None finally: loop.close() async def update_persona_prompt(persona: dict): """Update a persona's chat prompt with latest facts and memories.""" - # Get latest facts and user info facts = get_facts(persona['uid'], limit=250) user_name = get_user_name(persona['uid']) @@ -533,7 +534,7 @@ async def update_persona_prompt(persona: dict): # Add a guideline about tweets if they exist if condensed_tweets: - persona_prompt += "7. Utilize condensed tweets to enhance authenticity, incorporating common expressions, opinions, and phrasing from {user_name}’s social media presence.\n" + persona_prompt += "7. Utilize condensed tweets to enhance authenticity, incorporating common expressions, opinions, and phrasing from {user_name}'s social media presence.\n" persona_prompt += f""" **Rules:** @@ -558,6 +559,7 @@ async def update_persona_prompt(persona: dict): persona['persona_prompt'] = persona_prompt persona['updated_at'] = datetime.now(timezone.utc) + update_persona_in_db(persona) delete_app_cache_by_id(persona['id']) diff --git a/backend/utils/memories/process_memory.py b/backend/utils/memories/process_memory.py index 4bd6ad34bc6..9c75debde8b 100644 --- a/backend/utils/memories/process_memory.py +++ b/backend/utils/memories/process_memory.py @@ -21,7 +21,7 @@ from models.task import Task, TaskStatus, TaskAction, TaskActionProvider from models.trend import Trend from models.notification_message import NotificationMessage -from utils.apps import get_available_apps +from utils.apps import get_available_apps, sync_update_persona_prompt from utils.llm import obtain_emotional_message, retrieve_metadata_fields_from_transcript, \ summarize_open_glass, get_transcript_structure, generate_embedding, \ get_plugin_result, should_discard_memory, summarize_experience_text, new_facts_extractor, \ @@ -208,6 +208,19 @@ def save_structured_vector(uid: str, memory: Memory, update_only: bool = False): update_vector_metadata(uid, memory.id, metadata) +def _update_personas_async(uid: str): + print(f"[PERSONAS] Starting persona updates in background thread for uid={uid}") + personas = get_omi_personas_by_uid_db(uid) + if personas: + threads = [] + for persona in personas: + threads.append(threading.Thread(target=sync_update_persona_prompt, args=(persona,))) + + [t.start() for t in threads] + [t.join() for t in threads] + print(f"[PERSONAS] Finished persona updates in background thread for uid={uid}") + + def process_memory( uid: str, language_code: str, memory: Union[Memory, CreateMemory, ExternalIntegrationCreateMemory], force_process: bool = False, is_reprocess: bool = False @@ -227,15 +240,9 @@ def process_memory( threading.Thread(target=memory_created_webhook, args=(uid, memory,)).start() # TODO: Bad code, cause the websocket was drop, need to check it carefully before enabling. # Update persona prompts with new memory - # personas = get_omi_personas_by_uid_db(uid) - # if personas: - # threads = [] - # print('updating personas after memory creation') - # for persona in personas: - # threads.append(threading.Thread(target=sync_update_persona_prompt, args=(persona,))) - # - # [t.start() for t in threads] - # [t.join() for t in threads] + print("before creating the thread for _update_personas_async") + threading.Thread(target=_update_personas_async, args=(uid,)).start() + print("after calling start for _update_personas_async") # TODO: trigger external integrations here too diff --git a/backend/utils/social.py b/backend/utils/social.py index d02917aa598..1cd0afd54d8 100644 --- a/backend/utils/social.py +++ b/backend/utils/social.py @@ -39,6 +39,7 @@ async def get_twitter_profile(handle: str) -> Dict[str, Any]: "X-RapidAPI-Host": rapid_api_host } + def fetch_profile(): response = httpx.get(url, headers=headers, timeout=defaultTimeoutSec) if response.status_code == 200: