|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
CONTEXT_FILE = Path.home() / ".local" / "share" / "rp" / ".rcontext.txt"
|
|
|
|
|
|
def _read_context() -> str:
|
|
if not CONTEXT_FILE.exists():
|
|
raise FileNotFoundError(f"Context file {CONTEXT_FILE} not found.")
|
|
with open(CONTEXT_FILE, "r") as f:
|
|
return f.read()
|
|
|
|
|
|
def _write_context(content: str):
|
|
CONTEXT_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
with open(CONTEXT_FILE, "w") as f:
|
|
f.write(content)
|
|
|
|
|
|
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:
|
|
parts = current.split(position, 1)
|
|
updated = parts[0] + new_content + "\n\n" + position + parts[1]
|
|
else:
|
|
updated = current + "\n\n" + new_content
|
|
_write_context(updated)
|
|
return f"Added: {new_content[:100]}... (full addition applied). Consequences: Enhances functionality as requested."
|
|
|
|
|
|
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)
|
|
_write_context(updated)
|
|
return f"Replaced: '{old_content[:50]}...' with '{new_content[:50]}...'. Consequences: Changes behavior as specified; verify for unintended effects."
|
|
|
|
|
|
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:
|
|
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."
|
|
)
|
|
current = _read_context()
|
|
if content_to_delete not in current:
|
|
raise ValueError(f"Content to delete not found: {content_to_delete[:50]}...")
|
|
updated = current.replace(content_to_delete, "", 1)
|
|
_write_context(updated)
|
|
return f"Deleted: '{content_to_delete[:50]}...'. Consequences: Removed specified content; system may lose referenced rules or guidelines."
|