Standard library only · runs in your browser
Trimming (stage 7) throws old turns away. Long-term memory keeps the important facts outside the context window and brings back only the ones relevant to the current question, newest first when facts conflict.
Implement recall(memories, query, k=3). Each memory is {"id": str, "turn": int, "text": str}.
re.findall(r"[a-z0-9]+", ...), and drop single-character words (the "s" in "user's") and words in the provided STOPWORDS set. Use a set, so repeats count once.turn (newer first), then id (alphabetical).k.recall(memories: list[dict], query: str, k: int = 3) -> list[str]