""" System prompt manager module Manages and updates system prompts, including fast initialization and async enhancement """ import os import asyncio import threading from typing import Dict, Any, TYPE_CHECKING if TYPE_CHECKING: from core.connection import ConnectionHandler from config.logger import setup_logging from jinja2 import Template TAG = __name__ EMOJI_List = [ "😶", "🙂", "😆", "😂", "😔", "😠", "😭", "😍", "😳", "😲", "😱", "🤔", "😉", "😎", "😌", "🤤", "😘", "😏", "😴", "😜", "🙄", ] class PromptManager: """System prompt manager; responsible for managing and updating system prompts""" def __init__(self, config: Dict[str, Any], logger=None): self.config = config self.logger = logger or setup_logging() self.base_prompt_template = None self.last_update_time = 0 # Import global cache manager from core.utils.cache.manager import cache_manager, CacheType self.cache_manager = cache_manager self.CacheType = CacheType # Initialize context source from core.utils.context_provider import ContextDataProvider self.context_provider = ContextDataProvider(config, self.logger) self.context_data = {} self._load_base_template() def _load_base_template(self): """Load base prompt template""" try: template_path = self.config.get("prompt_template", None) if not template_path: template_path = "agent-base-prompt.txt" cache_key = f"prompt_template:{template_path}" # Fetch from cache first cached_template = self.cache_manager.get(self.CacheType.CONFIG, cache_key) if cached_template is not None: self.base_prompt_template = cached_template self.logger.bind(tag=TAG).debug("Load base prompt template from cache") return # Cache miss, read from file if os.path.exists(template_path): with open(template_path, "r", encoding="utf-8") as f: template_content = f.read() # Store in cache (CONFIG type does not auto-expire by default; manual invalidation required) self.cache_manager.set( self.CacheType.CONFIG, cache_key, template_content ) self.base_prompt_template = template_content self.logger.bind(tag=TAG).debug("Successfully loaded and cached base prompt template") else: self.logger.bind(tag=TAG).warning(f"{template_path} file not found") except Exception as e: self.logger.bind(tag=TAG).error(f"Failed to load prompt template: {e}") def get_quick_prompt(self, user_prompt: str, device_id: str = None) -> str: """Quickly retrieve system prompt (using user config)""" device_cache_key = f"device_prompt:{device_id}" cached_device_prompt = self.cache_manager.get( self.CacheType.DEVICE_PROMPT, device_cache_key ) if cached_device_prompt is not None: self.logger.bind(tag=TAG).debug(f"Use cached prompt for device {device_id}") return cached_device_prompt else: self.logger.bind(tag=TAG).debug( f"Device {device_id} has no cached prompt; using the provided prompt" ) # Use provided prompt and cache it (if device ID exists) if device_id: device_cache_key = f"device_prompt:{device_id}" self.cache_manager.set(self.CacheType.DEVICE_PROMPT, device_cache_key, user_prompt) self.logger.bind(tag=TAG).debug(f"Prompt for device {device_id} cached") self.logger.bind(tag=TAG).info(f"Using quick prompt: {user_prompt[:50]}...") return user_prompt def _get_current_time_info(self) -> tuple: """Get current time info""" from .current_time import ( get_current_date, get_current_weekday, get_current_lunar_date, ) today_date = get_current_date() today_weekday = get_current_weekday() lunar_date = get_current_lunar_date() + "\n" return today_date, today_weekday, lunar_date def _get_location_info(self, client_ip: str) -> str: """Get location info""" try: # Fetch from cache first cached_location = self.cache_manager.get(self.CacheType.LOCATION, client_ip) if cached_location is not None: return cached_location # Cache miss, call API to fetch from core.utils.util import get_ip_info ip_info = get_ip_info(client_ip, self.logger) city = ip_info.get("city", "Unknown location") location = f"{city}" # Store in cache self.cache_manager.set(self.CacheType.LOCATION, client_ip, location) return location except Exception as e: self.logger.bind(tag=TAG).error(f"Failed to get location info: {e}") return "Unknown location" def _get_weather_info(self, conn: "ConnectionHandler", location: str) -> str: """Get weather info""" try: # Fetch from cache first cached_weather = self.cache_manager.get(self.CacheType.WEATHER, location) if cached_weather is not None: return cached_weather # Cache miss, calling async get_weather function # Windows ProactorEventLoop does not support run_coroutine_threadsafe().result() # Therefore use call_soon_threadsafe to submit task + threading.Event to wait for result # Note: Event.wait() blocks only the current thread pool thread, not the main event loop from plugins_func.functions.get_weather import get_weather from plugins_func.register import ActionResponse result_holder = [] exception_holder = [] async def _call(): try: result_holder.append( await get_weather(conn, location=location, lang="en_US") ) except Exception as e: exception_holder.append(e) finally: event.set() event = threading.Event() conn.loop.call_soon_threadsafe(lambda: asyncio.ensure_future(_call())) if not event.wait(timeout=10): raise TimeoutError("Weather info retrieval timed out") if exception_holder: raise exception_holder[0] result = result_holder[0] if isinstance(result, ActionResponse): weather_report = result.result self.cache_manager.set(self.CacheType.WEATHER, location, weather_report) return weather_report return "Failed to fetch weather information" except Exception as e: self.logger.bind(tag=TAG).error(f"Failed to get weather info: {e}") return "Failed to fetch weather information" def update_context_info(self, conn, client_ip: str): """Sync context information""" try: local_address = "" if ( client_ip and self.base_prompt_template and ( "local_address" in self.base_prompt_template or "weather_info" in self.base_prompt_template ) ): # Get location info (using global cache) local_address = self._get_location_info(client_ip) if ( self.base_prompt_template and "weather_info" in self.base_prompt_template and local_address ): # Get weather info (using global cache) self._get_weather_info(conn, local_address) # Get configured context data if hasattr(conn, "device_id") and conn.device_id: if ( self.base_prompt_template and "dynamic_context" in self.base_prompt_template ): self.context_data = self.context_provider.fetch_all(conn.device_id) else: self.context_data = "" self.logger.bind(tag=TAG).debug(f"Context info updated") except Exception as e: self.logger.bind(tag=TAG).error(f"Failed to update context information: {e}") def build_enhanced_prompt( self, user_prompt: str, device_id: str, client_ip: str = None, *args, **kwargs ) -> str: """Build enhanced system prompt""" if not self.base_prompt_template: return user_prompt try: # Get the latest time info (no cache) today_date, today_weekday, lunar_date = self._get_current_time_info() # Get cached context information local_address = "" weather_info = "" if client_ip: # Get location info (from global cache) local_address = ( self.cache_manager.get(self.CacheType.LOCATION, client_ip) or "" ) # Get weather info (from global cache) if local_address: weather_info = ( self.cache_manager.get(self.CacheType.WEATHER, local_address) or "" ) # Get TTS selected language, default is Chinese language = ( self.config.get("TTS", {}) .get(self.config.get("selected_module", {}).get("TTS", ""), {}) .get("language") or "Chinese" ) self.logger.bind(tag=TAG).debug(f"Selected language retrieved: {language}") # Replace template variables template = Template(self.base_prompt_template) enhanced_prompt = template.render( base_prompt=user_prompt, current_time="{{current_time}}", today_date=today_date, today_weekday=today_weekday, lunar_date=lunar_date, local_address=local_address, weather_info=weather_info, emojiList=EMOJI_List, device_id=device_id, client_ip=client_ip, dynamic_context=self.context_data, language=language, *args, **kwargs, ) device_cache_key = f"device_prompt:{device_id}" self.cache_manager.set( self.CacheType.DEVICE_PROMPT, device_cache_key, enhanced_prompt ) self.logger.bind(tag=TAG).info( f"Enhanced prompt built successfully, length: {len(enhanced_prompt)}" ) return enhanced_prompt except Exception as e: self.logger.bind(tag=TAG).error(f"Failed to build enhanced prompt: {e}") return user_prompt