Return session metadata dicts, most recently updated first.
(kb_dir: Path)
| 194 | |
| 195 | |
| 196 | def list_sessions(kb_dir: Path) -> list[dict[str, Any]]: |
| 197 | """Return session metadata dicts, most recently updated first.""" |
| 198 | d = chats_dir(kb_dir) |
| 199 | if not d.exists(): |
| 200 | return [] |
| 201 | out: list[dict[str, Any]] = [] |
| 202 | for p in d.glob("*.json"): |
| 203 | try: |
| 204 | data = json.loads(p.read_text(encoding="utf-8")) |
| 205 | except (json.JSONDecodeError, OSError): |
| 206 | continue |
| 207 | out.append( |
| 208 | { |
| 209 | "id": data.get("id", p.stem), |
| 210 | "title": data.get("title", ""), |
| 211 | "turn_count": data.get("turn_count", 0), |
| 212 | "updated_at": data.get("updated_at", ""), |
| 213 | "model": data.get("model", ""), |
| 214 | } |
| 215 | ) |
| 216 | out.sort(key=lambda s: (s["updated_at"], s["id"]), reverse=True) |
| 217 | return out |
| 218 | |
| 219 | |
| 220 | def resolve_session_id(kb_dir: Path, query: str) -> str | None: |
no test coverage detected