Stand-in import of elffuss/claw
One parentless commit with only the files that the open seed tasks touch and the modules they import. The full squashed import (SPEC 8.6) is a later step: this tree is not the cutoff tree. T2T-StandIn-Source-Commit: 70b800e87ef05a9574bf2ae8226cef90015dbe21
This commit is contained in:
32
web/js/db.js
Normal file
32
web/js/db.js
Normal file
@@ -0,0 +1,32 @@
|
||||
// IndexedDB mínima: stores clave→valor.
|
||||
const NAME = 'elffuss', VERSION = 2;
|
||||
const STORES = ['kv', 'apps', 'tasks', 'vault', 'fs', 'memory'];
|
||||
let dbPromise = null;
|
||||
|
||||
function open() {
|
||||
if (dbPromise) return dbPromise;
|
||||
dbPromise = new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open(NAME, VERSION);
|
||||
req.onupgradeneeded = () => {
|
||||
for (const s of STORES)
|
||||
if (!req.result.objectStoreNames.contains(s)) req.result.createObjectStore(s);
|
||||
};
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
return dbPromise;
|
||||
}
|
||||
|
||||
function tx(store, mode, fn) {
|
||||
return open().then(db => new Promise((resolve, reject) => {
|
||||
const req = fn(db.transaction(store, mode).objectStore(store));
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
}));
|
||||
}
|
||||
|
||||
export const get = (store, key) => tx(store, 'readonly', s => s.get(key));
|
||||
export const set = (store, key, val) => tx(store, 'readwrite', s => s.put(val, key));
|
||||
export const del = (store, key) => tx(store, 'readwrite', s => s.delete(key));
|
||||
export const keys = (store) => tx(store, 'readonly', s => s.getAllKeys());
|
||||
export const all = (store) => tx(store, 'readonly', s => s.getAll());
|
||||
112
web/js/runtime/caps.js
Normal file
112
web/js/runtime/caps.js
Normal file
@@ -0,0 +1,112 @@
|
||||
// Elffuss Runtime · sonda de capacidades del dispositivo.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// PRIMER módulo de nuestro runtime propio. El nudo del problema «cargar modelos
|
||||
// grandes» es saber QUÉ cabe en cada equipo:
|
||||
// · Escritorio (GPU potente, RAM amplia): modelos grandes, pero hay que
|
||||
// shardear los pesos porque WebGPU limita el tamaño de cada buffer.
|
||||
// · Móvil: RAM total limitada y el navegador mata la pestaña por encima de
|
||||
// ~1-2 GB → techo real ~1-2 GB por mucho que la GPU sea potente.
|
||||
// Esta sonda reporta límites de WebGPU + memoria + un «presupuesto de modelo»
|
||||
// realista por plataforma. La usan la selección de cerebro y el loader por shards.
|
||||
|
||||
const GiB = 1024 ** 3;
|
||||
const MiB = 1024 ** 2;
|
||||
|
||||
// Detección de plataforma SIN depender de cadenas frágiles: primero el
|
||||
// userAgentData estructurado, luego el userAgent como respaldo. Solo se lee el
|
||||
// entorno del USUARIO para decidir qué correr (feature-detection), no se registra.
|
||||
function detectPlatform() {
|
||||
const uad = navigator.userAgentData;
|
||||
const ua = navigator.userAgent || '';
|
||||
const touch = (navigator.maxTouchPoints || 0) > 1;
|
||||
const iOS = /iPhone|iPad|iPod/i.test(ua) ||
|
||||
// iPad moderno se hace pasar por Mac: Mac + pantalla táctil = iPad.
|
||||
(/Mac/i.test(ua) && touch);
|
||||
const android = /Android/i.test(ua);
|
||||
const mobile = !!(uad?.mobile) || iOS || android ||
|
||||
matchMedia('(max-width: 820px)').matches || matchMedia('(pointer: coarse)').matches;
|
||||
const mac = /Mac/i.test(uad?.platform || ua) && !iOS;
|
||||
return { mobile, iOS, android, mac, touch };
|
||||
}
|
||||
|
||||
// Presupuesto de modelo realista (bytes de PESOS que el equipo puede sostener,
|
||||
// dejando margen para activaciones + KV + el propio navegador).
|
||||
// · iOS: el navegador revienta la pestaña sobre ~1.3 GB de datos vivos.
|
||||
// · Android: variable; tiramos conservador.
|
||||
// · Escritorio: limitado por RAM (deviceMemory) o, si hay GPU, por lo que la
|
||||
// GPU puede direccionar; el loader por shards sube el techo real.
|
||||
function modelBudget(platform, memGB) {
|
||||
if (platform.iOS) return Math.round(1.3 * GiB);
|
||||
if (platform.android) return Math.round(1.0 * GiB);
|
||||
if (platform.mobile) return Math.round(1.0 * GiB);
|
||||
// escritorio: ~60 % de la RAM declarada, tope de seguridad 12 GB
|
||||
return Math.min(Math.round(memGB * 0.6 * GiB), 12 * GiB);
|
||||
}
|
||||
|
||||
// Tamaño máximo de un SHARD de pesos en la GPU: nunca puede exceder el binding
|
||||
// de storage buffer del adaptador (a menudo 128 MiB–2 GiB). El loader partirá los
|
||||
// tensores grandes en trozos de este tamaño. Se deja un pelín por debajo del
|
||||
// límite para las estructuras auxiliares.
|
||||
function shardCap(limits) {
|
||||
const bind = limits?.maxStorageBufferBindingSize || 128 * MiB;
|
||||
// usar como mucho el binding, y no más de 256 MiB por shard (equilibrio entre
|
||||
// nº de bind groups y presión de memoria); mínimo 16 MiB.
|
||||
return Math.max(16 * MiB, Math.min(bind, 256 * MiB));
|
||||
}
|
||||
|
||||
export async function probe() {
|
||||
const platform = detectPlatform();
|
||||
// deviceMemory viene redondeado (2,4,8…) y en móvil suele mentir a la baja;
|
||||
// si no está, asumimos 4 en móvil y 8 en escritorio.
|
||||
const memGB = navigator.deviceMemory || (platform.mobile ? 4 : 8);
|
||||
|
||||
let gpu = false, limits = null, info = null, error = null;
|
||||
if (navigator.gpu) {
|
||||
try {
|
||||
const adapter = await navigator.gpu.requestAdapter({ powerPreference: 'high-performance' });
|
||||
if (adapter) {
|
||||
gpu = true;
|
||||
const L = adapter.limits || {};
|
||||
limits = {
|
||||
maxBufferSize: L.maxBufferSize || 0,
|
||||
maxStorageBufferBindingSize: L.maxStorageBufferBindingSize || 0,
|
||||
maxComputeWorkgroupStorageSize: L.maxComputeWorkgroupStorageSize || 0,
|
||||
maxComputeInvocationsPerWorkgroup: L.maxComputeInvocationsPerWorkgroup || 0,
|
||||
maxComputeWorkgroupSizeX: L.maxComputeWorkgroupSizeX || 0,
|
||||
};
|
||||
// requestAdapterInfo está deprecándose a favor de adapter.info; probamos ambos.
|
||||
try { info = adapter.info || (adapter.requestAdapterInfo ? await adapter.requestAdapterInfo() : null); } catch { /* opcional */ }
|
||||
} else {
|
||||
error = 'navigator.gpu existe pero no hay adaptador real';
|
||||
}
|
||||
} catch (e) { error = String(e?.message || e); }
|
||||
} else {
|
||||
error = 'sin WebGPU';
|
||||
}
|
||||
|
||||
const budget = modelBudget(platform, memGB);
|
||||
return {
|
||||
platform,
|
||||
memGB,
|
||||
gpu,
|
||||
limits,
|
||||
info, // {vendor, architecture, device, description} si el navegador lo da
|
||||
error,
|
||||
budgetBytes: budget,
|
||||
budgetGiB: +(budget / GiB).toFixed(2),
|
||||
shardCapBytes: shardCap(limits),
|
||||
// ¿entra un modelo de N bytes de pesos? (el loader por shards salva el límite
|
||||
// de buffer; el muro real es el presupuesto de memoria del equipo).
|
||||
fits(weightBytes) { return gpu && weightBytes <= budget; },
|
||||
};
|
||||
}
|
||||
|
||||
// Resumen legible de una línea para diagnóstico en la UI (sin datos sensibles).
|
||||
export function summarize(c) {
|
||||
const p = c.platform.iOS ? 'iOS' : c.platform.android ? 'Android'
|
||||
: c.platform.mac ? 'Mac' : c.platform.mobile ? 'móvil' : 'escritorio';
|
||||
const g = c.gpu
|
||||
? `WebGPU · buffer≤${(c.limits.maxBufferSize / GiB).toFixed(1)}GiB · binding≤${(c.limits.maxStorageBufferBindingSize / MiB) | 0}MiB`
|
||||
: `sin GPU (${c.error || '—'})`;
|
||||
return `${p} · ~${c.memGB}GB RAM · presupuesto ${c.budgetGiB}GiB · ${g}`;
|
||||
}
|
||||
191
web/js/skills.js
Normal file
191
web/js/skills.js
Normal file
@@ -0,0 +1,191 @@
|
||||
// Skills de Elffuss Code: instrucciones en markdown (formato SKILL.md de
|
||||
// Claude Code) que el modelo sigue cuando aplican. Se instalan desde el
|
||||
// catálogo grande OFICIAL (github.com/anthropics/skills), desde los plugins
|
||||
// oficiales, o desde CUALQUIER repo público (marketplaces de la comunidad
|
||||
// tipo OpenClaude/openclaw). Todo transparente: se ve el repo, la lista y el
|
||||
// SKILL.md que se inyecta. Se guardan en IndexedDB (nada sale del navegador).
|
||||
import * as db from './db.js';
|
||||
|
||||
const KEY = 'skills'; // skills instaladas
|
||||
const SRC_KEY = 'skills.sources'; // repos personalizados añadidos por el usuario
|
||||
const MAX_SKILL = 12_000; // caracteres del cuerpo que se inyectan al modelo
|
||||
|
||||
export const CATALOG_REPO = 'anthropics/skills';
|
||||
export const DEFAULT_SOURCES = [
|
||||
{ repo: 'KikoCis/elffuss-claw-skills', label: 'Elffuss · Skills que funcionan en Claw (3D·Excel·web·juegos)', official: true },
|
||||
{ repo: 'bbgnsurftech/claude-skills-collection', label: 'Comunidad · Mega-colección (1000+ skills)', official: true },
|
||||
{ repo: 'anthropics/skills', label: 'Anthropic · Agent Skills (oficial de Claude Code)', official: true },
|
||||
{ repo: 'anthropics/claude-plugins-official', label: 'Anthropic · Claude Code Plugins (oficial)', official: true },
|
||||
{ repo: 'earthtojake/text-to-cad', label: 'Text-to-CAD · CAD/STEP/DXF (Python, solo referencia)', official: true },
|
||||
{ repo: 'scottstts/Threejs-Awesome-Graphics-Agent-Skills', label: '3D en el navegador · Three.js (funciona en Claw)', official: true },
|
||||
];
|
||||
|
||||
let cache = []; // instaladas, en memoria (para que el systemPrompt sea síncrono)
|
||||
|
||||
// `loaded` distingue «no hay skills» de «no se pudo leer»: si la lectura
|
||||
// falla, cache queda vacío pero loaded=false, y install() se niega a persistir
|
||||
// (antes, un fallo transitorio de IndexedDB borraba TODAS las skills al
|
||||
// guardar la siguiente).
|
||||
let loaded = false;
|
||||
export async function initSkills() {
|
||||
try {
|
||||
const stored = await db.get('kv', KEY);
|
||||
cache = Array.isArray(stored) ? stored : [];
|
||||
loaded = true;
|
||||
} catch (e) {
|
||||
cache = [];
|
||||
loaded = false;
|
||||
console.warn('[elffuss] no se pudieron leer las skills; no las sobrescribiré', e);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
export function skillsLoaded() { return loaded; }
|
||||
|
||||
export async function all() { return cache; }
|
||||
export function installed() { return cache; }
|
||||
export function isInstalled(repo, path) { return cache.some(s => s.repo === repo && s.path === path); }
|
||||
|
||||
export async function install(skill) {
|
||||
// Si la carga inicial falló, cache no representa lo instalado: reintentar
|
||||
// leer antes de escribir. Sin esto, guardar aquí borraría las skills reales.
|
||||
if (!loaded) {
|
||||
await initSkills();
|
||||
if (!loaded) throw new Error('No pude leer tus skills guardadas; no instalo para no perderlas. Recarga la página e inténtalo otra vez.');
|
||||
}
|
||||
cache = cache.filter(s => !(s.repo === skill.repo && s.path === skill.path) && s.name !== skill.name);
|
||||
const entry = { ...skill, content: (skill.content || '').slice(0, MAX_SKILL) };
|
||||
cache.push(entry);
|
||||
await db.set('kv', KEY, cache);
|
||||
return entry; // devuelve la skill completa (name, description…)
|
||||
}
|
||||
|
||||
// Mensaje «cómo usarla» tras instalar: qué hace + un ejemplo de qué pedir.
|
||||
export function usageMessage(skill) {
|
||||
const desc = (skill.description || '').trim();
|
||||
const ejemplo = firstExample(skill) || `algo relacionado con «${skill.name}»`;
|
||||
return `✳ **Skill «${skill.name}» instalada.**\n\n` +
|
||||
(desc ? desc + '\n\n' : '') +
|
||||
`Ya la sigo en cada conversación. Para usarla, pídeme por ejemplo:\n\n> ${ejemplo}`;
|
||||
}
|
||||
|
||||
// Intenta sacar un ejemplo de uso del cuerpo del SKILL.md (líneas de ejemplo/uso).
|
||||
function firstExample(skill) {
|
||||
const body = skill.content || '';
|
||||
const m = body.match(/(?:ejemplo|example|uso|usage|prueba|try)[^\n:]*[::]\s*["“]?([^\n"”]{6,90})/i)
|
||||
|| body.match(/^[-*]\s+([A-ZÁÉÍÓÚ][^\n]{10,80})/m);
|
||||
return m ? m[1].trim().replace(/[.*_`]+$/, '') : null;
|
||||
}
|
||||
|
||||
export async function remove(nameOrPath) {
|
||||
cache = cache.filter(s => s.name !== nameOrPath && s.path !== nameOrPath);
|
||||
await db.set('kv', KEY, cache);
|
||||
}
|
||||
|
||||
export async function get(name) {
|
||||
return cache.find(s => s.name.toLowerCase() === String(name).toLowerCase()) || null;
|
||||
}
|
||||
|
||||
// ---- fuentes (repos) ----
|
||||
export async function sources() {
|
||||
const custom = (await db.get('kv', SRC_KEY).catch(() => null)) || [];
|
||||
return [...DEFAULT_SOURCES, ...custom];
|
||||
}
|
||||
export async function addSource(repoOrUrl, label) {
|
||||
const repo = repoOrUrl.trim().replace(/^https?:\/\/github\.com\//, '').replace(/\.git$/, '').replace(/\/$/, '');
|
||||
if (!/^[\w.-]+\/[\w.-]+$/.test(repo)) throw new Error('Usa owner/repo (o la URL de GitHub)');
|
||||
const custom = (await db.get('kv', SRC_KEY).catch(() => null)) || [];
|
||||
if (!custom.some(s => s.repo === repo) && !DEFAULT_SOURCES.some(s => s.repo === repo))
|
||||
custom.push({ repo, label: label || repo });
|
||||
await db.set('kv', SRC_KEY, custom);
|
||||
return repo;
|
||||
}
|
||||
export async function removeSource(repo) {
|
||||
const custom = (await db.get('kv', SRC_KEY).catch(() => null)) || [];
|
||||
await db.set('kv', SRC_KEY, custom.filter(s => s.repo !== repo));
|
||||
}
|
||||
|
||||
// ---- catálogo desde GitHub ----
|
||||
// Una sola llamada al árbol git del repo → todos los SKILL.md.
|
||||
export async function listFromRepo(repo) {
|
||||
let tree, branch;
|
||||
for (const b of ['main', 'master']) {
|
||||
const r = await fetch(`https://api.github.com/repos/${repo}/git/trees/${b}?recursive=1`);
|
||||
if (r.ok) { tree = await r.json(); branch = b; break; }
|
||||
if (r.status === 403) throw new Error('GitHub limitó las peticiones (60/h sin login). Reintenta en unos minutos.');
|
||||
}
|
||||
if (!tree) throw new Error('No pude leer el repo (¿existe y es público?)');
|
||||
return (tree.tree || [])
|
||||
.filter(n => /(^|\/)SKILL\.md$/i.test(n.path))
|
||||
.map(n => ({ repo, branch, path: n.path, dir: n.path.replace(/\/SKILL\.md$/i, ''), name: n.path.replace(/\/SKILL\.md$/i, '').split('/').pop() || repo }))
|
||||
.sort((a, b) => a.dir.localeCompare(b.dir));
|
||||
}
|
||||
|
||||
// Descarga el SKILL.md y lo instala (frontmatter YAML simple).
|
||||
export async function installFromRepo(entry) {
|
||||
let md = null;
|
||||
for (const b of [entry.branch, 'main', 'master'].filter(Boolean)) {
|
||||
const r = await fetch(`https://raw.githubusercontent.com/${entry.repo}/${b}/${entry.path}`);
|
||||
if (r.ok) { md = await r.text(); break; }
|
||||
}
|
||||
if (md == null) throw new Error('No pude descargar el SKILL.md');
|
||||
const skill = parseSkill(md, entry.name);
|
||||
return install({ ...skill, repo: entry.repo, path: entry.path });
|
||||
}
|
||||
|
||||
// SKILL.md → { name, description, content }
|
||||
export function parseSkill(md, fallbackName = 'skill') {
|
||||
const fm = md.match(/^---\n([\s\S]*?)\n---/);
|
||||
const meta = fm?.[1] || '';
|
||||
const name = meta.match(/^name:\s*(.+)$/m)?.[1]?.trim() || fallbackName;
|
||||
const description = (meta.match(/^description:\s*([\s\S]+?)(?:\n\w+:|$)/m)?.[1] || '').replace(/\s+/g, ' ').trim();
|
||||
const content = md.replace(/^---\n[\s\S]*?\n---\n?/, '').trim();
|
||||
return { name, description, content };
|
||||
}
|
||||
|
||||
// Creador de skills: Elffuss fabrica una skill propia (SKILL.md) a partir de
|
||||
// lo que el usuario quiere, y la instala al instante. Aparece en la pestaña
|
||||
// Skills y se inyecta en el prompt de los siguientes turnos.
|
||||
export async function createSkill({ name, description, instructions } = {}) {
|
||||
if (!name || !instructions) throw new Error('Faltan name o instructions');
|
||||
const skill = {
|
||||
name: String(name).slice(0, 48),
|
||||
description: (description || '').slice(0, 240),
|
||||
content: String(instructions).slice(0, MAX_SKILL),
|
||||
repo: 'creada por Elffuss',
|
||||
path: 'local/' + Date.now(),
|
||||
};
|
||||
await install(skill);
|
||||
return `Skill «${skill.name}» creada e instalada. Ya la sigo en cada conversación (mírala en la pestaña Skills).`;
|
||||
}
|
||||
|
||||
// Editar una skill YA instalada: reemplaza sus instrucciones (o las añade con append),
|
||||
// conservando su origen. Para ajustar el script/argumentos de una skill existente.
|
||||
export async function editSkill({ name, description, instructions, append } = {}) {
|
||||
if (!name) throw new Error('Falta name');
|
||||
const s = cache.find(x => x.name.toLowerCase() === String(name).toLowerCase());
|
||||
if (!s) throw new Error(`No hay ninguna skill «${name}». Créala primero con skill.create.`);
|
||||
let content = s.content || '';
|
||||
if (instructions != null) content = append ? (content + '\n' + instructions) : String(instructions);
|
||||
const updated = { ...s, description: description != null ? String(description).slice(0, 240) : s.description, content: content.slice(0, MAX_SKILL) };
|
||||
await install(updated);
|
||||
return `Skill «${s.name}» actualizada. Ya sigo la nueva versión.`;
|
||||
}
|
||||
|
||||
// Bloque para el systemPrompt (síncrono, desde la caché).
|
||||
export function skillsPromptBlock() {
|
||||
if (!cache.length) return '';
|
||||
const nombres = cache.map(s => `«${s.name}»`).join(', ');
|
||||
const parts = cache.map(s =>
|
||||
`### Skill «${s.name}»${s.repo ? ` (de ${s.repo})` : ''}\n${s.description || ''}\n${(s.content || '').slice(0, MAX_SKILL)}`);
|
||||
// Framing DIRECTIVO: un modelo pequeño con el framing pasivo de antes negaba
|
||||
// tener la skill aunque estuviera inyectada («I don't have that skill»). Aquí
|
||||
// se le dice explícitamente que ES una capacidad suya AHORA, que no la niegue,
|
||||
// que empareje por tema aunque el usuario use otro nombre, y que si la skill
|
||||
// trae una plantilla de app la cree con app.create copiándola.
|
||||
return `\n\nSKILLS INSTALADAS — son capacidades que TIENES AHORA MISMO: ${nombres}.\n` +
|
||||
`REGLAS con las skills:\n` +
|
||||
`- Si la petición encaja con una skill (aunque el usuario la nombre con otras palabras o un nombre parecido, empareja por el TEMA de su descripción), ÚSALA siguiendo sus instrucciones al pie de la letra.\n` +
|
||||
`- NUNCA digas que no tienes esa skill o esa capacidad: la tienes, está aquí abajo.\n` +
|
||||
`- Si la skill incluye una plantilla de app (HTML), créala con la herramienta app.create COPIANDO esa plantilla y adaptando solo lo que la skill indique — no inventes una app genérica distinta.\n\n` +
|
||||
parts.join('\n\n');
|
||||
}
|
||||
Reference in New Issue
Block a user