|
| 1 | +"""Saveable preset profiles for runtime retrieval settings.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import dataclasses |
| 6 | +import json |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from pathlib import Path |
| 11 | + |
| 12 | + from core.config import ConversationRuntimeConfig |
| 13 | + |
| 14 | +PROFILE_FIELDS: list[str] = [ |
| 15 | + "use_mmr", |
| 16 | + "rag_rerank_enabled", |
| 17 | + "rag_sentence_compression_enabled", |
| 18 | + "rag_multi_query_enabled", |
| 19 | + "rag_k", |
| 20 | + "rag_k_mes", |
| 21 | + "debug_context", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +class ProfileStore: |
| 26 | + """Persist and apply named retrieval-setting presets stored in a JSON file.""" |
| 27 | + |
| 28 | + def __init__(self, path: Path) -> None: |
| 29 | + self._path = path |
| 30 | + |
| 31 | + def _load(self) -> dict[str, dict[str, object]]: |
| 32 | + if not self._path.exists(): |
| 33 | + return {} |
| 34 | + try: |
| 35 | + data = json.loads(self._path.read_text(encoding="utf-8")) |
| 36 | + return data if isinstance(data, dict) else {} |
| 37 | + except Exception: |
| 38 | + return {} |
| 39 | + |
| 40 | + def _save(self, data: dict[str, dict[str, object]]) -> None: |
| 41 | + self._path.parent.mkdir(parents=True, exist_ok=True) |
| 42 | + self._path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8") |
| 43 | + |
| 44 | + def list_profiles(self) -> list[str]: |
| 45 | + """Return sorted list of saved profile names.""" |
| 46 | + return sorted(self._load().keys()) |
| 47 | + |
| 48 | + def save_profile(self, name: str, config: ConversationRuntimeConfig) -> None: |
| 49 | + """Snapshot the profile-eligible fields from *config* under *name*.""" |
| 50 | + data = self._load() |
| 51 | + data[name] = {field: getattr(config, field) for field in PROFILE_FIELDS} |
| 52 | + self._save(data) |
| 53 | + |
| 54 | + def get_profile(self, name: str) -> dict[str, object]: |
| 55 | + """Return the stored settings dict for *name*.""" |
| 56 | + data = self._load() |
| 57 | + if name not in data: |
| 58 | + msg = f"Profile {name!r} not found" |
| 59 | + raise KeyError(msg) |
| 60 | + return dict(data[name]) |
| 61 | + |
| 62 | + def apply_profile( |
| 63 | + self, name: str, config: ConversationRuntimeConfig |
| 64 | + ) -> tuple[ConversationRuntimeConfig, list[str]]: |
| 65 | + """Return a new config with profile values applied and list of changed field names.""" |
| 66 | + profile = self.get_profile(name) |
| 67 | + validated_updates: dict[str, object] = {} |
| 68 | + changed: list[str] = [] |
| 69 | + for field, value in profile.items(): |
| 70 | + if field not in PROFILE_FIELDS: |
| 71 | + continue |
| 72 | + current = getattr(config, field, None) |
| 73 | + if current != value: |
| 74 | + validated_updates[field] = value |
| 75 | + changed.append(field) |
| 76 | + if validated_updates: |
| 77 | + config = dataclasses.replace(config, **validated_updates) |
| 78 | + return config, changed |
| 79 | + |
| 80 | + def delete_profile(self, name: str) -> None: |
| 81 | + """Remove *name* from the store (no-op if not found).""" |
| 82 | + data = self._load() |
| 83 | + data.pop(name, None) |
| 84 | + self._save(data) |
| 85 | + |
| 86 | + def current_values(self, config: ConversationRuntimeConfig) -> dict[str, object]: |
| 87 | + """Return current values of the profile-eligible fields from *config*.""" |
| 88 | + return {field: getattr(config, field) for field in PROFILE_FIELDS} |
0 commit comments