"""Command recording and library management.""" from typing import List, Optional from datetime import datetime from .models import Command from .database import Database class CommandLibrary: """Manages the personal command library.""" def __init__(self, db: Database): self.db = db def add(self, command: str, description: str = "", tags: Optional[List[str]] = None) -> Optional[int]: cmd = Command( command=command, description=description, tags=tags or [], created_at=datetime.now(), ) return self.db.add_command(cmd) def list(self, limit: int = 50) -> List[Command]: commands = self.db.get_all_commands() return commands[:limit] def search(self, query: str, limit: int = 10) -> List[Command]: return self.db.search_commands(query, limit) def get(self, command_id: int) -> Optional[Command]: return self.db.get_command(command_id) def delete(self, command_id: int) -> bool: return self.db.delete_command(command_id) def increment_usage(self, command_id: int): self.db.update_command_usage(command_id) def find_similar(self, command: str, threshold: int = 60) -> List[tuple]: from fuzzywuzzy import fuzz all_commands = self.db.get_all_commands() similar = [] for cmd in all_commands: ratio = fuzz.ratio(command.lower(), cmd.command.lower()) if ratio >= threshold: similar.append((cmd, ratio)) return sorted(similar, key=lambda x: x[1], reverse=True)