Save conversation history to disk. Keeps last max_turns turns.
(history: list, project_dir: str = None, max_turns: int = 6)
| 41 | return SESSIONS_DIR / f"{name}_{key}.json" |
| 42 | |
| 43 | def save_session(history: list, project_dir: str = None, max_turns: int = 6): |
| 44 | """Save conversation history to disk. Keeps last max_turns turns.""" |
| 45 | if not history: |
| 46 | return |
| 47 | path = _session_path(project_dir) |
| 48 | # Keep only recent history and redact secrets |
| 49 | keep = history[-max_turns * 2:] |
| 50 | safe_history = [] |
| 51 | for turn in keep: |
| 52 | safe_turn = turn.copy() |
| 53 | safe_turn["content"] = redact_secrets(turn["content"]) |
| 54 | safe_history.append(safe_turn) |
| 55 | |
| 56 | data = { |
| 57 | "saved_at": datetime.now().isoformat(), |
| 58 | "project": project_dir or os.getcwd(), |
| 59 | "turns": len(safe_history) // 2, |
| 60 | "history": safe_history, |
| 61 | } |
| 62 | try: |
| 63 | path.write_text(json.dumps(data, indent=2), encoding="utf-8") |
| 64 | except Exception as e: |
| 65 | warning(f"Could not save session: {e}") |
| 66 | |
| 67 | def load_session(project_dir: str = None, path: str = None, max_age_hours: int = 2) -> list: |
| 68 | """Load conversation history from disk. Returns empty list if stale or missing.""" |
no test coverage detected