2025-12-07 17:15:09 +02:00
# utils/context_manager.py
"""
Structured context management for Miku ' s personality and knowledge.
Replaces the vector search system with organized , complete context .
Preserves original content files in their entirety .
2026-01-23 15:02:36 +02:00
When LANGUAGE_MODE is " japanese " , appends a Japanese instruction to ensure
all responses are in Japanese without requiring separate files .
2025-12-07 17:15:09 +02:00
"""
2026-01-23 15:02:36 +02:00
import globals
feat: Implement comprehensive non-hierarchical logging system
- Created new logging infrastructure with per-component filtering
- Added 6 log levels: DEBUG, INFO, API, WARNING, ERROR, CRITICAL
- Implemented non-hierarchical level control (any combination can be enabled)
- Migrated 917 print() statements across 31 files to structured logging
- Created web UI (system.html) for runtime configuration with dark theme
- Added global level controls to enable/disable levels across all components
- Added timestamp format control (off/time/date/datetime options)
- Implemented log rotation (10MB per file, 5 backups)
- Added API endpoints for dynamic log configuration
- Configured HTTP request logging with filtering via api.requests component
- Intercepted APScheduler logs with proper formatting
- Fixed persistence paths to use /app/memory for Docker volume compatibility
- Fixed checkbox display bug in web UI (enabled_levels now properly shown)
- Changed System Settings button to open in same tab instead of new window
Components: bot, api, api.requests, autonomous, persona, vision, llm,
conversation, mood, dm, scheduled, gpu, media, server, commands,
sentiment, core, apscheduler
All settings persist across container restarts via JSON config.
2026-01-10 20:46:19 +02:00
from utils . logger import get_logger
logger = get_logger ( ' core ' )
2025-12-07 17:15:09 +02:00
def get_original_miku_lore ( ) - > str :
""" Load the complete, unmodified miku_lore.txt file """
try :
with open ( " miku_lore.txt " , " r " , encoding = " utf-8 " ) as f :
return f . read ( )
except Exception as e :
feat: Implement comprehensive non-hierarchical logging system
- Created new logging infrastructure with per-component filtering
- Added 6 log levels: DEBUG, INFO, API, WARNING, ERROR, CRITICAL
- Implemented non-hierarchical level control (any combination can be enabled)
- Migrated 917 print() statements across 31 files to structured logging
- Created web UI (system.html) for runtime configuration with dark theme
- Added global level controls to enable/disable levels across all components
- Added timestamp format control (off/time/date/datetime options)
- Implemented log rotation (10MB per file, 5 backups)
- Added API endpoints for dynamic log configuration
- Configured HTTP request logging with filtering via api.requests component
- Intercepted APScheduler logs with proper formatting
- Fixed persistence paths to use /app/memory for Docker volume compatibility
- Fixed checkbox display bug in web UI (enabled_levels now properly shown)
- Changed System Settings button to open in same tab instead of new window
Components: bot, api, api.requests, autonomous, persona, vision, llm,
conversation, mood, dm, scheduled, gpu, media, server, commands,
sentiment, core, apscheduler
All settings persist across container restarts via JSON config.
2026-01-10 20:46:19 +02:00
logger . error ( f " Failed to load miku_lore.txt: { e } " )
2025-12-07 17:15:09 +02:00
return " ## MIKU LORE \n [File could not be loaded] "
def get_original_miku_prompt ( ) - > str :
""" Load the complete, unmodified miku_prompt.txt file """
try :
with open ( " miku_prompt.txt " , " r " , encoding = " utf-8 " ) as f :
return f . read ( )
except Exception as e :
feat: Implement comprehensive non-hierarchical logging system
- Created new logging infrastructure with per-component filtering
- Added 6 log levels: DEBUG, INFO, API, WARNING, ERROR, CRITICAL
- Implemented non-hierarchical level control (any combination can be enabled)
- Migrated 917 print() statements across 31 files to structured logging
- Created web UI (system.html) for runtime configuration with dark theme
- Added global level controls to enable/disable levels across all components
- Added timestamp format control (off/time/date/datetime options)
- Implemented log rotation (10MB per file, 5 backups)
- Added API endpoints for dynamic log configuration
- Configured HTTP request logging with filtering via api.requests component
- Intercepted APScheduler logs with proper formatting
- Fixed persistence paths to use /app/memory for Docker volume compatibility
- Fixed checkbox display bug in web UI (enabled_levels now properly shown)
- Changed System Settings button to open in same tab instead of new window
Components: bot, api, api.requests, autonomous, persona, vision, llm,
conversation, mood, dm, scheduled, gpu, media, server, commands,
sentiment, core, apscheduler
All settings persist across container restarts via JSON config.
2026-01-10 20:46:19 +02:00
logger . error ( f " Failed to load miku_prompt.txt: { e } " )
2025-12-07 17:15:09 +02:00
return " ## MIKU PROMPT \n [File could not be loaded] "
def get_original_miku_lyrics ( ) - > str :
""" Load the complete, unmodified miku_lyrics.txt file """
try :
with open ( " miku_lyrics.txt " , " r " , encoding = " utf-8 " ) as f :
return f . read ( )
except Exception as e :
feat: Implement comprehensive non-hierarchical logging system
- Created new logging infrastructure with per-component filtering
- Added 6 log levels: DEBUG, INFO, API, WARNING, ERROR, CRITICAL
- Implemented non-hierarchical level control (any combination can be enabled)
- Migrated 917 print() statements across 31 files to structured logging
- Created web UI (system.html) for runtime configuration with dark theme
- Added global level controls to enable/disable levels across all components
- Added timestamp format control (off/time/date/datetime options)
- Implemented log rotation (10MB per file, 5 backups)
- Added API endpoints for dynamic log configuration
- Configured HTTP request logging with filtering via api.requests component
- Intercepted APScheduler logs with proper formatting
- Fixed persistence paths to use /app/memory for Docker volume compatibility
- Fixed checkbox display bug in web UI (enabled_levels now properly shown)
- Changed System Settings button to open in same tab instead of new window
Components: bot, api, api.requests, autonomous, persona, vision, llm,
conversation, mood, dm, scheduled, gpu, media, server, commands,
sentiment, core, apscheduler
All settings persist across container restarts via JSON config.
2026-01-10 20:46:19 +02:00
logger . error ( f " Failed to load miku_lyrics.txt: { e } " )
2025-12-07 17:15:09 +02:00
return " ## MIKU LYRICS \n [File could not be loaded] "
2026-01-23 15:02:36 +02:00
def _get_japanese_instruction ( ) - > str :
"""
Returns the Japanese language instruction to append to context .
Ensures all responses are in Japanese when in Japanese mode .
"""
return " \n \n [CRITICAL INSTRUCTION - 重要な指示] \n **YOU MUST RESPOND ENTIRELY IN JAPANESE (日本語). NO ENGLISH ALLOWED.** \n すべての返答は必ず日本語で行ってください。英語での返答は一切禁止されています。 \n これは最優先の指示です。必ず守ってください。 "
2025-12-07 17:15:09 +02:00
def get_complete_context ( ) - > str :
2026-01-23 15:02:36 +02:00
"""
Returns all essential Miku context using original files in their entirety .
If LANGUAGE_MODE is " japanese " , appends a Japanese instruction to ensure
all responses are in Japanese .
"""
lore = get_original_miku_lore ( )
prompt = get_original_miku_prompt ( )
lyrics = get_original_miku_lyrics ( )
combined = f """ ## MIKU LORE (Complete Original)
{ lore }
2025-12-07 17:15:09 +02:00
## MIKU PERSONALITY & GUIDELINES (Complete Original)
2026-01-23 15:02:36 +02:00
{ prompt }
2025-12-07 17:15:09 +02:00
## MIKU SONG LYRICS (Complete Original)
2026-01-23 15:02:36 +02:00
{ lyrics } """
# Append Japanese instruction if in Japanese mode
if globals . LANGUAGE_MODE == " japanese " :
combined + = _get_japanese_instruction ( )
logger . info ( f " [core] Context loaded in { globals . LANGUAGE_MODE } mode " )
return combined
2025-12-07 17:15:09 +02:00
def get_context_for_response_type ( response_type : str ) - > str :
2026-01-23 15:02:36 +02:00
"""
Returns appropriate context based on the type of response being generated .
If LANGUAGE_MODE is " japanese " , appends Japanese instruction to all contexts
to ensure responses are in Japanese .
"""
lore = get_original_miku_lore ( )
prompt = get_original_miku_prompt ( )
lyrics = get_original_miku_lyrics ( )
2025-12-07 17:15:09 +02:00
2026-01-23 15:02:36 +02:00
# Build core context (always in English source files)
2025-12-07 17:15:09 +02:00
core_context = f """ ## MIKU LORE (Complete Original)
2026-01-23 15:02:36 +02:00
{ lore }
2025-12-07 17:15:09 +02:00
## MIKU PERSONALITY & GUIDELINES (Complete Original)
2026-01-23 15:02:36 +02:00
{ prompt } """
2025-12-07 17:15:09 +02:00
2026-01-23 15:02:36 +02:00
# Return context based on response type
2025-12-07 17:15:09 +02:00
if response_type == " autonomous_general " :
2026-01-23 15:02:36 +02:00
context = f """ { core_context }
2025-12-07 17:15:09 +02:00
## MIKU SONG LYRICS (Complete Original)
2026-01-23 15:02:36 +02:00
{ lyrics } """
2025-12-07 17:15:09 +02:00
elif response_type == " autonomous_tweet " :
2026-01-23 15:02:36 +02:00
context = f """ { core_context }
2025-12-07 17:15:09 +02:00
## MIKU SONG LYRICS (Complete Original)
2026-01-23 15:02:36 +02:00
{ lyrics } """
2025-12-07 17:15:09 +02:00
elif response_type == " dm_response " or response_type == " server_response " :
2026-01-23 15:02:36 +02:00
context = f """ { core_context }
2025-12-07 17:15:09 +02:00
## MIKU SONG LYRICS (Complete Original)
2026-01-23 15:02:36 +02:00
{ lyrics } """
2025-12-07 17:15:09 +02:00
elif response_type == " conversation_join " :
2026-01-23 15:02:36 +02:00
context = f """ { core_context }
2025-12-07 17:15:09 +02:00
## MIKU SONG LYRICS (Complete Original)
2026-01-23 15:02:36 +02:00
{ lyrics } """
2025-12-07 17:15:09 +02:00
elif response_type == " emoji_selection " :
2026-01-23 15:02:36 +02:00
# For emoji reactions, minimal context needed
context = " "
2025-12-07 17:15:09 +02:00
else :
# Default: comprehensive context
2026-01-23 15:02:36 +02:00
context = get_complete_context ( )
# Append Japanese instruction if in Japanese mode
if globals . LANGUAGE_MODE == " japanese " and context :
context + = _get_japanese_instruction ( )
return context