Files
DeepTutor/deeptutor_cli/memory.py
Bingxi Zhao (Frank) 77f29cbcc7 fix lint
2026-04-23 19:29:16 +08:00

74 lines
2.4 KiB
Python

"""
CLI memory commands for the two-file public memory system (SUMMARY/PROFILE).
"""
from __future__ import annotations
from typing import cast
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
import typer
from deeptutor.services.memory import get_memory_service
from deeptutor.services.memory.service import MemoryFile
console = Console()
def register(app: typer.Typer) -> None:
@app.command("show")
def memory_show(
file: str = typer.Argument(
"all",
help="File to show: summary, profile, or all.",
),
) -> None:
"""Display memory file content."""
svc = get_memory_service()
if file == "all":
snap = svc.read_snapshot()
for label, content in [
("SUMMARY", snap.summary),
("PROFILE", snap.profile),
]:
if content:
console.print(Panel(Markdown(content), title=f"[bold]{label}.md[/]"))
else:
console.print(f"[dim]{label}.md: (empty)[/]")
elif file in ("summary", "profile"):
content = svc.read_file(cast(MemoryFile, file))
if content:
console.print(Panel(Markdown(content), title=f"[bold]{file.upper()}.md[/]"))
else:
console.print(f"[dim]{file.upper()}.md: (empty)[/]")
else:
console.print(f"[red]Unknown file: {file}. Use summary, profile, or all.[/]")
@app.command("clear")
def memory_clear(
file: str = typer.Argument(
"all",
help="File to clear: summary, profile, or all.",
),
force: bool = typer.Option(False, "--force", "-f", help="Skip confirmation."),
) -> None:
"""Clear memory file(s)."""
svc = get_memory_service()
if file not in ("summary", "profile", "all"):
console.print(f"[red]Unknown file: {file}[/]")
raise typer.Exit(1)
if not force:
target = "all memory files" if file == "all" else f"{file.upper()}.md"
if not typer.confirm(f"Clear {target}?"):
raise typer.Abort()
if file == "all":
svc.clear_memory()
console.print("[green]Cleared all memory files.[/]")
else:
svc.clear_file(cast(MemoryFile, file))
console.print(f"[green]Cleared {file.upper()}.md.[/]")