utopia-ia/elffuss-code public
Changesets · Branches · Tags · Compare
| Changeset | 4eb6a3c2f5a87f2dc9ee6d0c34831f5d6a2f658d1ce91e34289c98b60bc1c7fa |
|---|---|
| Author | KikoCis <kikocisneros@gmail.com> · 2026-10-10 16:19 +02:00 |
| Committer | KikoCis <kikocisneros@gmail.com> · 2026-10-11 14:13 +02:00 |
| Parents | 9255fd1f0dc0 |
4 files changed
modified web/js/db.js +12 −2
| @@ -25,8 +25,18 @@ | ||
| 25 | 25 | })); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | +// Quien necesite saber que algo se escribió (el espacio de trabajo, que copia | |
| 29 | +// conversaciones y skills a tu disco con «Guardar solo») se apunta aquí. Un | |
| 30 | +// fallo de quien escucha nunca rompe la escritura. | |
| 31 | +const oyentes = []; | |
| 32 | +export function onWrite(fn) { | |
| 33 | + oyentes.push(fn); | |
| 34 | + return () => { const i = oyentes.indexOf(fn); if (i >= 0) oyentes.splice(i, 1); }; | |
| 35 | +} | |
| 36 | +const avisa = (store, key) => (r) => { for (const f of oyentes) { try { f(store, key); } catch { /* */ } } return r; }; | |
| 37 | + | |
| 28 | 38 | export const get = (store, key) => tx(store, 'readonly', s => s.get(key)); |
| 29 | -export const set = (store, key, val) => tx(store, 'readwrite', s => s.put(val, key)); | |
| 30 | -export const del = (store, key) => tx(store, 'readwrite', s => s.delete(key)); | |
| 39 | +export const set = (store, key, val) => tx(store, 'readwrite', s => s.put(val, key)).then(avisa(store, key)); | |
| 40 | +export const del = (store, key) => tx(store, 'readwrite', s => s.delete(key)).then(avisa(store, key)); | |
| 31 | 41 | export const keys = (store) => tx(store, 'readonly', s => s.getAllKeys()); |
| 32 | 42 | export const all = (store) => tx(store, 'readonly', s => s.getAll()); |
modified web/js/model-cache.js +89 −0
| @@ -49,3 +49,92 @@ | ||
| 49 | 49 | navigator.serviceWorker.controller.postMessage('clear-models'); |
| 50 | 50 | try { await caches.delete('elffuss-models-v1'); } catch { /* */ } |
| 51 | 51 | } |
| 52 | + | |
| 53 | +// ── Lo que ocupan los MODELOS, y vaciarlos de verdad ───────────────────────── | |
| 54 | +// El chip 💾 y «Vaciar caché» medían `navigator.storage.estimate()` (el origen | |
| 55 | +// ENTERO: conversaciones, el proyecto de prueba en OPFS…) y lo llamaban | |
| 56 | +// «modelos guardados», y al vaciar solo borraban la Cache Storage vieja. Pero | |
| 57 | +// los pesos viven en otros sitios: la carpeta OPFS del almacén de modelos | |
| 58 | +// (runtime/model-store.js), la caché de transformers.js (el modelo ONNX) y el | |
| 59 | +// almacén COMPARTIDO del broker, que está en otro origen. Ahora se cuenta y se | |
| 60 | +// vacía cada uno, y se dice lo que de verdad se liberó. | |
| 61 | +const MODEL_CACHES = ['elffuss-models-v1', 'transformers-cache']; | |
| 62 | +const MODEL_DIR = 'elffuss-models'; | |
| 63 | + | |
| 64 | +async function opfsModelBytes() { | |
| 65 | + try { | |
| 66 | + const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle(MODEL_DIR); | |
| 67 | + let n = 0; | |
| 68 | + for await (const e of dir.values()) if (e.kind === 'file') n += (await e.getFile()).size; | |
| 69 | + return n; | |
| 70 | + } catch { return 0; } // no existe todavía: nada guardado | |
| 71 | +} | |
| 72 | +async function cacheModelBytes() { | |
| 73 | + if (typeof caches === 'undefined') return 0; | |
| 74 | + let n = 0; | |
| 75 | + for (const name of await caches.keys().catch(() => [])) { | |
| 76 | + if (!MODEL_CACHES.includes(name) && !name.startsWith('transformers')) continue; | |
| 77 | + try { | |
| 78 | + const c = await caches.open(name); | |
| 79 | + for (const req of await c.keys()) { | |
| 80 | + const res = await c.match(req); | |
| 81 | + const len = +(res?.headers.get('content-length') || 0); | |
| 82 | + n += len || (res ? (await res.blob()).size : 0); | |
| 83 | + } | |
| 84 | + } catch { /* una caché ilegible no rompe la cuenta */ } | |
| 85 | + } | |
| 86 | + return n; | |
| 87 | +} | |
| 88 | +// El almacén compartido vive en el origen del broker: solo se puede medir y | |
| 89 | +// vaciar si el broker sabe hacerlo (sharedUsage / clearShared). Si no, se dice. | |
| 90 | +async function brokerApi() { | |
| 91 | + try { | |
| 92 | + const b = await import('./runtime/model-broker.js'); | |
| 93 | + if (typeof b.sharedUsage !== 'function') return null; | |
| 94 | + let origenes; | |
| 95 | + try { const r = await import('./engine/registro.js'); origenes = r.origenesAlmacen ? r.origenesAlmacen() : undefined; } | |
| 96 | + catch { /* sin motor: el broker por defecto */ } | |
| 97 | + return { b, origenes }; | |
| 98 | + } catch { return null; } | |
| 99 | +} | |
| 100 | + | |
| 101 | +// { local, shared, total, sharedOk }: `sharedOk` es false cuando el almacén | |
| 102 | +// compartido no se pudo consultar (puede haber más guardado del que se cuenta). | |
| 103 | +export async function modelUsage() { | |
| 104 | + const local = (await opfsModelBytes()) + (await cacheModelBytes()); | |
| 105 | + let shared = 0, sharedOk = false; | |
| 106 | + const api = await brokerApi(); | |
| 107 | + if (api) { | |
| 108 | + try { const r = await api.b.sharedUsage(api.origenes); shared = r.usage || 0; sharedOk = !!r.ok; } | |
| 109 | + catch { sharedOk = false; } | |
| 110 | + } | |
| 111 | + return { local, shared, total: local + shared, sharedOk }; | |
| 112 | +} | |
| 113 | + | |
| 114 | +// Vacía TODO lo que son pesos de modelo y devuelve cuánto se liberó DE VERDAD | |
| 115 | +// (se mide antes y después), más lo que no se pudo tocar. | |
| 116 | +export async function clearModels() { | |
| 117 | + const antes = await modelUsage(); | |
| 118 | + const fallos = []; | |
| 119 | + if (navigator.serviceWorker?.controller) navigator.serviceWorker.controller.postMessage('clear-models'); | |
| 120 | + if (typeof caches !== 'undefined') { | |
| 121 | + for (const name of await caches.keys().catch(() => [])) { | |
| 122 | + if (!MODEL_CACHES.includes(name) && !name.startsWith('transformers')) continue; | |
| 123 | + try { await caches.delete(name); } catch (e) { fallos.push(name + ': ' + (e?.message || e)); } | |
| 124 | + } | |
| 125 | + } | |
| 126 | + try { | |
| 127 | + const { clearAll } = await import('./runtime/model-store.js'); | |
| 128 | + await clearAll(); | |
| 129 | + } catch (e) { fallos.push('OPFS: ' + (e?.message || e)); } | |
| 130 | + const api = await brokerApi(); | |
| 131 | + if (api && typeof api.b.clearShared === 'function') { | |
| 132 | + const lista = Array.isArray(api.origenes) && api.origenes.length ? [...new Set(api.origenes)] : [undefined]; | |
| 133 | + for (const o of lista) { | |
| 134 | + try { const r = await api.b.clearShared(o); if (!r?.ok) fallos.push('broker ' + (o || '')); } | |
| 135 | + catch (e) { fallos.push('broker ' + (o || '') + ': ' + (e?.message || e)); } | |
| 136 | + } | |
| 137 | + } | |
| 138 | + const despues = await modelUsage(); | |
| 139 | + return { liberado: Math.max(0, antes.total - despues.total), antes, despues, fallos, sharedOk: antes.sharedOk }; | |
| 140 | +} |
modified web/js/telemetry.js +7 −3
| @@ -23,10 +23,13 @@ | ||
| 23 | 23 | if (enabled) hookGlobalErrors(); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | +// Devuelve si el servidor lo aceptó DE VERDAD (respuesta 2xx). Antes no | |
| 27 | +// devolvía nada y el panel de feedback decía «¡enviado!» aunque el servidor | |
| 28 | +// contestara 404 o no hubiera red. | |
| 26 | 29 | async function post(kind, message, opts = {}) { |
| 27 | - if (!enabled) return; | |
| 30 | + if (!enabled) return false; | |
| 28 | 31 | try { |
| 29 | - await fetch('/proxy/report', { | |
| 32 | + const res = await fetch('/proxy/report', { | |
| 30 | 33 | method: 'POST', |
| 31 | 34 | headers: { 'Content-Type': 'application/json' }, |
| 32 | 35 | body: JSON.stringify({ |
| @@ -36,7 +39,8 @@ | ||
| 36 | 39 | extra: String(opts.extra || '').slice(0, 2000), |
| 37 | 40 | }), |
| 38 | 41 | }); |
| 39 | - } catch { /* sin conexión o proxy caído: se descarta, no hay reintento */ } | |
| 42 | + return res.ok; | |
| 43 | + } catch { return false; /* sin conexión o proxy caído: se descarta, no hay reintento */ } | |
| 40 | 44 | } |
| 41 | 45 | |
| 42 | 46 | export function reportError(message, opts = {}) { return post('error', message, opts); } |
modified web/js/workspace.js +9 −5
| @@ -38,9 +38,13 @@ | ||
| 38 | 38 | const emit = () => { const s = statusSync(); listeners.forEach(f => { try { f(s); } catch { /* */ } }); }; |
| 39 | 39 | export function onChange(fn) { listeners.push(fn); return () => { listeners = listeners.filter(f => f !== fn); }; } |
| 40 | 40 | |
| 41 | +// La raíz del almacenamiento privado del navegador (OPFS) no tiene nombre: '' | |
| 42 | +// se leía como «sin carpeta» y la carpeta elegida no se podía usar. | |
| 43 | +const nombreDe = h => h ? (h.name || 'OPFS (navegador)') : null; | |
| 44 | + | |
| 41 | 45 | function statusSync() { |
| 42 | 46 | return { |
| 43 | - supported: supported(), folder: dirHandle ? dirHandle.name : null, ready, | |
| 47 | + supported: supported(), folder: nombreDe(dirHandle), ready, | |
| 44 | 48 | dirty: [...dirty], lastSaveAt, lastError, autosave: autosaveCfg.enabled, |
| 45 | 49 | }; |
| 46 | 50 | } |
| @@ -62,7 +66,7 @@ | ||
| 62 | 66 | dirHandle = h; |
| 63 | 67 | ready = await perm(h); |
| 64 | 68 | emit(); |
| 65 | - return { name: h.name, ready, reason: ready ? null : 'prompt' }; | |
| 69 | + return { name: nombreDe(h), ready, reason: ready ? null : 'prompt' }; | |
| 66 | 70 | } |
| 67 | 71 | |
| 68 | 72 | // Elegir carpeta (requiere gesto de usuario). |
| @@ -73,7 +77,7 @@ | ||
| 73 | 77 | ready = true; |
| 74 | 78 | await cfg.db.set('kv', HANDLE_KEY, h); |
| 75 | 79 | emit(); |
| 76 | - return { name: h.name, ready: true }; | |
| 80 | + return { name: nombreDe(h), ready: true }; | |
| 77 | 81 | } |
| 78 | 82 | |
| 79 | 83 | // Reutilizar un handle que la app ya tiene (Code: la carpeta del proyecto). |
| @@ -83,7 +87,7 @@ | ||
| 83 | 87 | ready = await perm(handle); |
| 84 | 88 | await cfg.db.set('kv', HANDLE_KEY, handle); |
| 85 | 89 | emit(); |
| 86 | - return { name: handle.name, ready }; | |
| 90 | + return { name: nombreDe(handle), ready }; | |
| 87 | 91 | } |
| 88 | 92 | |
| 89 | 93 | // Re-conceder permiso tras recargar (requiere gesto de usuario). |
| @@ -95,7 +99,7 @@ | ||
| 95 | 99 | ready = ok; |
| 96 | 100 | emit(); |
| 97 | 101 | if (!ok) throw new Error('permiso denegado'); |
| 98 | - return { name: dirHandle.name, ready: true }; | |
| 102 | + return { name: nombreDe(dirHandle), ready: true }; | |
| 99 | 103 | } |
| 100 | 104 | |
| 101 | 105 | export async function forget() { |