2025-11-06 15:15:06 +01:00
import os
from typing import Optional
2025-11-06 16:44:41 +01:00
CONTEXT_FILE = " /home/retoor/.local/share/rp/.rcontext.txt "
2025-11-06 15:15:06 +01:00
def _read_context ( ) - > str :
if not os . path . exists ( CONTEXT_FILE ) :
raise FileNotFoundError ( f " Context file { CONTEXT_FILE } not found. " )
2025-11-06 16:44:41 +01:00
with open ( CONTEXT_FILE , " r " ) as f :
2025-11-06 15:15:06 +01:00
return f . read ( )
2025-11-06 16:44:41 +01:00
2025-11-06 15:15:06 +01:00
def _write_context ( content : str ) :
2025-11-06 16:44:41 +01:00
with open ( CONTEXT_FILE , " w " ) as f :
2025-11-06 15:15:06 +01:00
f . write ( content )
2025-11-06 16:44:41 +01:00
2025-11-06 15:15:06 +01:00
def modify_context_add ( new_content : str , position : Optional [ str ] = None ) - > str :
"""
Add new content to the . rcontext . txt file .
Args :
new_content : The content to add .
position : Optional marker to insert before ( e . g . , ' *** ' ) .
"""
current = _read_context ( )
if position and position in current :
# Insert before the position
parts = current . split ( position , 1 )
2025-11-06 16:44:41 +01:00
updated = parts [ 0 ] + new_content + " \n \n " + position + parts [ 1 ]
2025-11-06 15:15:06 +01:00
else :
# Append at the end
2025-11-06 16:44:41 +01:00
updated = current + " \n \n " + new_content
2025-11-06 15:15:06 +01:00
_write_context ( updated )
return f " Added: { new_content [ : 100 ] } ... (full addition applied). Consequences: Enhances functionality as requested. "
2025-11-06 16:44:41 +01:00
2025-11-06 15:15:06 +01:00
def modify_context_replace ( old_content : str , new_content : str ) - > str :
"""
Replace old content with new content in . rcontext . txt .
Args :
old_content : The content to replace .
new_content : The replacement content .
"""
current = _read_context ( )
if old_content not in current :
raise ValueError ( f " Old content not found: { old_content [ : 50 ] } ... " )
updated = current . replace ( old_content , new_content , 1 ) # Replace first occurrence
_write_context ( updated )
return f " Replaced: ' { old_content [ : 50 ] } ... ' with ' { new_content [ : 50 ] } ... ' . Consequences: Changes behavior as specified; verify for unintended effects. "
2025-11-06 16:44:41 +01:00
2025-11-06 15:15:06 +01:00
def modify_context_delete ( content_to_delete : str , confirmed : bool = False ) - > str :
"""
Delete content from . rcontext . txt , but only if confirmed .
Args :
content_to_delete : The content to delete .
confirmed : Must be True to proceed with deletion .
"""
if not confirmed :
2025-11-06 16:44:41 +01:00
raise PermissionError (
f " Deletion not confirmed. To delete ' { content_to_delete [ : 50 ] } ... ' , you must explicitly confirm. Are you sure? This may affect system behavior permanently. "
)
2025-11-06 15:15:06 +01:00
current = _read_context ( )
if content_to_delete not in current :
raise ValueError ( f " Content to delete not found: { content_to_delete [ : 50 ] } ... " )
2025-11-06 16:44:41 +01:00
updated = current . replace ( content_to_delete , " " , 1 )
2025-11-06 15:15:06 +01:00
_write_context ( updated )
return f " Deleted: ' { content_to_delete [ : 50 ] } ... ' . Consequences: Removed specified content; system may lose referenced rules or guidelines. "