// static/js/slashAutocomplete.js // Lightweight popup that surfaces the existing /command registry as users // type. Reads COMMANDS from slashCommands.js — no command logic lives here. import { COMMANDS, LEGACY_ALIASES } from './slashCommands.js'; const POPUP_ID = 'slash-autocomplete'; const MAX_VISIBLE = 12; // Flatten the registry into a searchable list of leaf entries. Each entry is // either a top-level command or a "cmd sub" pair (so subcommands get their // own row when relevant — /toggle web, /chats new, etc). // Commands intentionally excluded from the autocomplete popup (pure easter // eggs with no productivity value, or internal machinery). const EXCLUDED = new Set(['flip','roll','8ball','fortune','odyssey','ascii']); // Important legacy aliases to promote to their own rows in the popup. These // are the short forms people will actually type (/new, /clear, /web, etc.) // rather than the full /chats new, /toggle web equivalents. const PROMOTED_ALIASES = new Set([ 'new','clear','rename','fork','export','archive','favorite','unfavorite', 'web','bash','research','doc', 'memories','forget', ]); function _flatten() { const out = []; const seen = new Set(); // 1. Top-level commands and their subcommands from COMMANDS for (const [name, def] of Object.entries(COMMANDS)) { if (EXCLUDED.has(name)) continue; if (def.hidden) continue; if (def.handler) { seen.add(`/${name}`); out.push({ token: `/${name}`, aliases: (def.alias || []).map(a => `/${a}`), category: def.category || '', help: def.help || '', usage: def.usage || '', }); } if (def.subs) { for (const [sub, sdef] of Object.entries(def.subs)) { if (sub.startsWith('_')) continue; if (sdef.hidden) continue; const tok = `/${name} ${sub}`; seen.add(tok); out.push({ token: tok, aliases: (sdef.alias || []).map(a => `/${name} ${a}`), category: def.category || '', help: sdef.help || '', usage: sdef.usage || '', }); } } } // 2. Promoted legacy aliases (/new, /clear, /web …) as convenient short rows if (LEGACY_ALIASES) { for (const [alias, { parent, sub }] of Object.entries(LEGACY_ALIASES)) { if (!PROMOTED_ALIASES.has(alias)) continue; const tok = `/${alias}`; if (seen.has(tok)) continue; const parentDef = COMMANDS[parent]; const subDef = parentDef?.subs?.[sub]; if (!subDef) continue; seen.add(tok); out.push({ token: tok, aliases: [], category: parentDef.category || '', help: subDef.help || '', usage: tok, }); } } return out; } function _scoreMatch(entry, query) { // query already starts with "/". Match against token + aliases. Prefix wins // over substring; alias match scores slightly lower than token match. const q = query.toLowerCase(); const t = entry.token.toLowerCase(); if (t === q) return 1000; if (t.startsWith(q)) return 500 + (50 - Math.min(50, t.length - q.length)); for (const a of entry.aliases) { const al = a.toLowerCase(); if (al === q) return 900; if (al.startsWith(q)) return 400; } if (t.includes(q)) return 100; if (entry.help.toLowerCase().includes(q.slice(1))) return 25; // help text return 0; } function _ensurePopup(textarea) { let el = document.getElementById(POPUP_ID); if (el) return el; el = document.createElement('div'); el.id = POPUP_ID; el.className = 'slash-autocomplete-popup'; el.setAttribute('role', 'listbox'); el.setAttribute('aria-label', 'Slash commands'); document.body.appendChild(el); return el; } function _position(popup, textarea) { const r = textarea.getBoundingClientRect(); const maxH = Math.min(window.innerHeight * 0.5, 360); popup.style.maxHeight = maxH + 'px'; // Anchor above the textarea, left-aligned with it popup.style.left = Math.round(r.left) + 'px'; popup.style.width = Math.max(280, Math.round(Math.min(r.width, 520))) + 'px'; // Place above when there's enough room, otherwise below. const aboveSpace = r.top; if (aboveSpace > maxH + 20) { popup.style.bottom = (window.innerHeight - r.top + 6) + 'px'; popup.style.top = ''; } else { popup.style.top = (r.bottom + 6) + 'px'; popup.style.bottom = ''; } } function _render(popup, items, selectedIdx, query) { if (!items.length) { popup.innerHTML = `
${_esc(query)}