diff --git a/site/index.html b/site/index.html index b522b07..c2b7c82 100644 --- a/site/index.html +++ b/site/index.html @@ -27,6 +27,7 @@ Dopamine menu One thing Brain dump + Done-Wall + + +
@@ -278,6 +291,24 @@ + +
@@ -352,6 +383,8 @@ + + diff --git a/site/static/app.js b/site/static/app.js index f600668..547acd6 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -475,6 +475,71 @@ timerPersist(); timerPaint(); } + /* ---------- AI Auto-Deconstruct Contract (Tier 1 & 2) ---------- */ + window.aiBreakdownTask = async function(taskText) { + // Shared Interface for Sibyl's Tier 3 + const prompt = `Break down this intimidating task into 3-5 tiny, actionable micro-steps: "${taskText}". Return only the list of steps.`; + + // Tier 3: WebLLM In-Browser (Advanced Opt-in) + // Checks if the user explicitly opted in to download the model + if (localStorage.getItem('ai_webllm_enabled') === 'true') { + try { + if (!window.webLLMEngine) { + // Initialize engine using the MLC WebLLM CDN + const { CreateMLCEngine } = await import('https://cdn.jsdelivr.net/npm/@mlc-ai/web-llm@0.2.46/+esm'); + // Use a 1B parameter model as decided: Llama-3.2-1B-Instruct + window.webLLMEngine = await CreateMLCEngine('Llama-3.2-1B-Instruct-q4f16_1-MLC', { + initProgressCallback: (progress) => { + // Hook this up to a UI later to show MB downloaded + console.log(`Downloading WebLLM model: ${progress.text}`); + } + }); + } + const reply = await window.webLLMEngine.chat.completions.create({ + messages: [{ role: 'user', content: prompt }] + }); + return reply.choices[0].message.content.split('\n').filter(s => s.trim().length > 0); + } catch (e) { + console.error('WebLLM Context Loss/OOM. Graded fallback to BYOK:', e); + // Graded fallback automatically drops through to Tier 2 + } + } + + // Tier 1: Browser Native (window.ai) + if (window.ai && window.ai.languageModel) { + try { + const caps = await window.ai.languageModel.capabilities(); + if (caps.available !== 'no') { + const session = await window.ai.languageModel.create(); + const result = await session.prompt(prompt); + return result.split('\n').filter(s => s.trim().length > 0); + } + } catch (e) { + console.warn("window.ai failed, falling back", e); + } + } + + // Tier 2: BYOK (Bring Your Own Key) Fallback + const key = store('byok_key'); + if (!key) { + throw new Error("NO_AI_AVAILABLE"); // Let UI prompt for key or let Tier 3 try + } + const resp = await fetch('https://api.openai.com/v1/chat/completions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + key + }, + body: JSON.stringify({ + model: 'gpt-4o-mini', + messages: [{ role: 'user', content: prompt }] + }) + }); + if (!resp.ok) throw new Error("API_ERROR"); + const data = await resp.json(); + return data.choices[0].message.content.split('\n').filter(s => s.trim().length > 0); + }; + $('#timer-reset').addEventListener('click', timerReset); $$('.preset').forEach(function (p) { @@ -509,6 +574,7 @@ $('#timer-complete').hidden = true; // Mark the current one-thing step as done in the breaker var nowData = store('now'); + var taskText = $('#now-text').textContent || (nowData && nowData.text) || ''; if (nowData && nowData.fromBreaker && typeof nowData.stepIndex === 'number') { var b = store(breakerKey); if (b && b.steps[nowData.stepIndex]) { @@ -517,6 +583,7 @@ breakerRender(); } } + handleNewDoneTasks(taskText); // Reset timer so it's fresh for the next step timerReset(); // Clear one-thing and show next step prompt @@ -719,8 +786,15 @@ $('#now-input').focus(); }); + function handleNewDoneTasks(taskString) { + if (window.dbRecordTask && taskString) { + window.dbRecordTask(taskString).then(() => { if (window.renderDoneWall) window.renderDoneWall(); }).catch(() => {}); + } + } + $('#now-done').addEventListener('click', function () { var nowData = store('now'); + var taskText = $('#now-text').textContent || (nowData && nowData.text) || ''; tickSound(); // If from breaker, mark that step done if (nowData && nowData.fromBreaker && typeof nowData.stepIndex === 'number') { @@ -731,6 +805,7 @@ breakerRender(); } } + handleNewDoneTasks(taskText); localStorage.removeItem('myadhd.now'); // Check if there's a next step from breaker var b2 = store(breakerKey); @@ -998,6 +1073,32 @@ /* ---------- keyboard shortcuts ---------- */ document.addEventListener('keydown', function (e) { + // Quick Capture (Alt+P) - active globally + if (e.altKey && (e.key === 'p' || e.key === 'P')) { + e.preventDefault(); + var overlay = document.getElementById('quick-capture-overlay'); + if (overlay.hidden) { + overlay.hidden = false; + document.getElementById('quick-capture-input').value = ''; + document.getElementById('quick-capture-input').focus(); + } else { + overlay.hidden = true; + } + return; + } + // Quick capture handling inside input + if (e.target.id === 'quick-capture-input') { + if (e.key === 'Escape') { + document.getElementById('quick-capture-overlay').hidden = true; + } else if (e.key === 'Enter' && e.target.value.trim() !== '') { + var unTasks = store('unsorted_tasks') || []; + unTasks.push({ text: e.target.value.trim(), created: Date.now() }); + store('unsorted_tasks', unTasks); + if (typeof unPaint === 'function') unPaint(); + document.getElementById('quick-capture-overlay').hidden = true; + e.target.value = ''; + } + } // Don't trigger shortcuts when typing in an input or contenteditable var tag = e.target.tagName; if (tag === 'INPUT' || tag === 'TEXTAREA' || e.target.contentEditable === 'true') return; @@ -1269,4 +1370,4 @@ if(timerTabBtn) timerTabBtn.click(); }); -})(); +})(); \ No newline at end of file diff --git a/site/static/done.js b/site/static/done.js new file mode 100644 index 0000000..32e57a0 --- /dev/null +++ b/site/static/done.js @@ -0,0 +1,90 @@ +window.renderDoneWall = function() { + if (!window.dbGetAllTasks) return; + window.dbGetAllTasks().then(tasks => { + var list = document.querySelector('#done-list'); + var empty = document.querySelector('#done-empty'); + if (!list || !empty) return; + + if (tasks.length === 0) { + list.innerHTML = ''; + empty.hidden = false; + return; + } + empty.hidden = true; + list.innerHTML = ''; + + // Group by Date string (local date) + const groups = {}; + const todayStr = new Date().toDateString(); + + tasks.forEach(t => { + let d = new Date(t.completedAt); + let dStr = d.toDateString(); + if (dStr === todayStr) dStr = "Today"; + else if (dStr === new Date(Date.now() - 86400000).toDateString()) dStr = "Yesterday"; + else dStr = d.toLocaleDateString(undefined, { weekday: 'long', month: 'short', day: 'numeric' }); + + if (!groups[dStr]) groups[dStr] = []; + groups[dStr].push(t); + }); + + Object.keys(groups).forEach(dateLabel => { + let head = document.createElement('h3'); + head.textContent = dateLabel; + head.style.marginTop = '1.5rem'; + head.style.marginBottom = '0.5rem'; + head.style.color = 'var(--text-muted)'; + list.appendChild(head); + + groups[dateLabel].forEach(task => { + let li = document.createElement('li'); + li.style.padding = '0.5rem 0'; + li.style.borderBottom = '1px solid var(--border)'; + + let row = document.createElement('div'); + row.style.display = 'flex'; + row.style.justifyContent = 'space-between'; + + let textSpan = document.createElement('span'); + textSpan.textContent = "✓ " + task.text; + textSpan.style.color = 'var(--primary)'; + + // Time + let timeSpan = document.createElement('span'); + let d = new Date(task.completedAt); + timeSpan.textContent = d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); + timeSpan.style.color = 'var(--text-muted)'; + timeSpan.style.fontSize = '0.85em'; + + row.appendChild(textSpan); + row.appendChild(timeSpan); + li.appendChild(row); + list.appendChild(li); + }); + }); + }).catch(e => console.error(e)); +}; + +document.addEventListener('DOMContentLoaded', () => { + var clearBtn = document.querySelector('#done-clear'); + if (clearBtn) { + clearBtn.addEventListener('click', () => { + if (window.confirm("Are you sure you want to clear your entire accomplishment history?")) { + if (window.dbClearTasks) { + window.dbClearTasks().then(() => { + if (window.renderDoneWall) window.renderDoneWall(); + }); + } + } + }); + } + + // Bind to tab switch + document.querySelectorAll('.tab').forEach(tab => { + tab.addEventListener('click', e => { + if (tab.dataset.tab === 'done') { + if (window.renderDoneWall) window.renderDoneWall(); + } + }); + }); +}); diff --git a/site/static/store.js b/site/static/store.js new file mode 100644 index 0000000..24de0ba --- /dev/null +++ b/site/static/store.js @@ -0,0 +1,65 @@ +const DB_NAME = 'MyADHDSyncDB'; +const DB_VERSION = 1; +const DONE_STORE_NAME = 'done_cache'; + +let db = null; + +function getStoreDB() { + return new Promise((resolve, reject) => { + if (db) return resolve(db); + const request = indexedDB.open(DB_NAME, DB_VERSION); + request.onupgradeneeded = (event) => { + const adb = event.target.result; + if (!adb.objectStoreNames.contains(DONE_STORE_NAME)) { + adb.createObjectStore(DONE_STORE_NAME, { keyPath: 'id', autoIncrement: true }); + } + }; + request.onsuccess = (event) => { + db = event.target.result; + resolve(db); + }; + request.onerror = (event) => reject(event.target.error); + }); +} + +window.dbRecordTask = function(text) { + return getStoreDB().then((db) => { + return new Promise((resolve, reject) => { + const tx = db.transaction(DONE_STORE_NAME, 'readwrite'); + const store = tx.objectStore(DONE_STORE_NAME); + const req = store.add({ + text: text, + completedAt: new Date().toISOString() + }); + req.onsuccess = () => resolve(); + req.onerror = () => reject(req.error); + }); + }); +}; + +window.dbGetAllTasks = function() { + return getStoreDB().then((db) => { + return new Promise((resolve, reject) => { + const tx = db.transaction(DONE_STORE_NAME, 'readonly'); + const store = tx.objectStore(DONE_STORE_NAME); + const req = store.getAll(); + req.onsuccess = () => { + const sorted = (req.result || []).sort((a,b) => new Date(b.completedAt) - new Date(a.completedAt)); + resolve(sorted); + }; + req.onerror = () => reject(req.error); + }); + }); +}; + +window.dbClearTasks = function() { + return getStoreDB().then((db) => { + return new Promise((resolve, reject) => { + const tx = db.transaction(DONE_STORE_NAME, 'readwrite'); + const store = tx.objectStore(DONE_STORE_NAME); + const req = store.clear(); + req.onsuccess = () => resolve(); + req.onerror = () => reject(req.error); + }); + }); +};