fix: AI cache fallback chain (git → sessionStorage → prompt)

This commit is contained in:
Kyrykbaev-I 2026-06-01 10:52:16 +05:00
parent 5b13383d5b
commit 8e568bb3e8

View File

@ -329,7 +329,7 @@ body { font-family: var(--font-base); background: var(--color-bg); color: var(--
<!-- Update section: only useful if you have DeepSeek key -->
<div class="ai-divider"></div>
<details class="ai-update-details">
<details class="ai-update-details" id="ai-update-details">
<summary class="ai-update-summary">🔄 Обновить анализ</summary>
<div class="ai-api-row" style="margin-top:10px;">
<div id="ai-key-note" class="ai-key-note hidden">✓ DeepSeek токен загружен из config.js</div>
@ -1209,22 +1209,40 @@ function restoreAiCache(){const c=sessionStorage.getItem('lastAiResponse');if(c)
/** Load AI response from ai-cache.json stored in the git repo */
async function loadAiCache() {
const dateEl = document.getElementById('ai-cache-date');
const dateEl = document.getElementById('ai-cache-date');
const details = document.getElementById('ai-update-details');
// 1. Try git-based shared cache
try {
const resp = await fetch(AI_CACHE_RAW, {cache:'no-cache'});
if (!resp.ok) { if(dateEl) dateEl.textContent = 'Кэш не найден'; return; }
const data = await resp.json();
if (!data.response) return;
AppState.lastAiResponse = data.response;
showAiResult(data.response);
if (dateEl && data.generated_at) {
const d = new Date(data.generated_at);
dateEl.textContent = 'Обновлено: ' + d.toLocaleDateString('ru-RU',{day:'numeric',month:'long',year:'numeric'});
if (resp.ok) {
const data = await resp.json();
if (data.response) {
AppState.lastAiResponse = data.response;
showAiResult(data.response);
if (dateEl && data.generated_at) {
const d = new Date(data.generated_at);
dateEl.textContent = 'Обновлено: ' + d.toLocaleDateString('ru-RU',{day:'numeric',month:'long',year:'numeric'});
}
return; // ✓ got shared cache — done
}
}
} catch(e) {
if(dateEl) dateEl.textContent = 'Кэш недоступен';
console.info('AI cache not loaded:', e.message);
console.info('AI git cache unavailable:', e.message);
}
// 2. Fallback: sessionStorage from this browser's previous session
const session = sessionStorage.getItem('lastAiResponse');
if (session) {
AppState.lastAiResponse = session;
showAiResult(session);
if (dateEl) dateEl.textContent = 'Из предыдущей сессии браузера';
return;
}
// 3. Nothing found — prompt user to generate
if (dateEl) dateEl.textContent = 'Анализ ещё не сгенерирован';
if (details) details.open = true; // auto-expand the refresh section
}
/** Save AI response to ai-cache.json in git via Gitea API.