From 9b18aad1c1a64a0be8091576265c438e5ce4b094 Mon Sep 17 00:00:00 2001 From: CalamitousFelicitousness Date: Fri, 10 Jul 2026 00:46:04 +0100 Subject: [PATCH] feat(civitai): record search filters in history entries History entries stored only the query or tag term, so restoring a search dropped every filter, and filter-only browsing was never recorded at all. - SearchHistory.add takes an optional params dict stored on the entry - get_search records non-default filters: type, sort, period, base models, nsfw, username, favorites - filter-only searches are recorded as 'filter' entries labeled with the filter values --- modules/civitai/api_civitai.py | 25 +++++++++++++++++++++++-- modules/civitai/userdata_civitai.py | 4 +++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/civitai/api_civitai.py b/modules/civitai/api_civitai.py index 4a8302757..5ccde0567 100644 --- a/modules/civitai/api_civitai.py +++ b/modules/civitai/api_civitai.py @@ -103,10 +103,31 @@ def get_search( base_models=bm_list, nsfw=nsfw, limit=limit, cursor=cursor, username=username, favorites=favorites, token=token, ) + history_params = {k: v for k, v in { + 'types': types, + 'sort': sort, + 'period': period, + 'base_models': base_models, + 'nsfw': nsfw, + 'username': username, + 'favorites': favorites, + }.items() if v} if query: - search_history.add('query', query) + search_history.add('query', query, history_params) elif tag: - search_history.add('tag', tag) + search_history.add('tag', tag, history_params) + elif history_params: + # Filter-only browse: label the entry with the filters themselves. + parts = [ + types, + base_models, + 'favorites' if favorites else (f'by {username}' if username else ''), + period, + sort, + 'nsfw' if nsfw else '', + ] + label = ' ยท '.join(p for p in parts if p) + search_history.add('filter', label, history_params) return response.dict(by_alias=True) diff --git a/modules/civitai/userdata_civitai.py b/modules/civitai/userdata_civitai.py index 64c7efae5..3d3c67f83 100644 --- a/modules/civitai/userdata_civitai.py +++ b/modules/civitai/userdata_civitai.py @@ -96,13 +96,15 @@ class SearchHistory: except Exception as e: log.error(f'CivitAI search history save error: file={path} {e}') - def add(self, search_type: str, term: str): + def add(self, search_type: str, term: str, params: dict | None = None): with self._lock: entry = { "type": search_type, "term": term, "timestamp": datetime.now().isoformat(), } + if params: + entry["params"] = params # Remove duplicate if same type+term exists self._entries = [e for e in self._entries if not (e.get('type') == search_type and e.get('term') == term)] self._entries.insert(0, entry)