import type { Skill, SkillRegistry } from '@moxxy/sdk'; import type { Logger } from '../logger.js'; export class SkillRegistryImpl implements SkillRegistry { private readonly byId = new Map(); private readonly byNameIdx = new Map(); private readonly logger?: Logger; constructor(opts: { logger?: Logger } = {}) { this.logger = opts.logger; } /** Warn (don't silently last-wins) when two distinct skills fight over one * frontmatter.name — otherwise byName() is non-deterministic by load order * and the first skill becomes unreachable by name. */ private warnNameCollision(name: string, incomingId: string): void { const existing = this.byNameIdx.get(name); if (existing && existing.id !== incomingId) { this.logger?.warn('SkillRegistry: two skills share a frontmatter name; last one wins by name', { name, existingId: existing.id, incomingId, }); } } list(): ReadonlyArray { return [...this.byId.values()]; } get(id: string): Skill | undefined { return this.byId.get(id); } byName(name: string): Skill | undefined { return this.byNameIdx.get(name); } filterByTriggers(prompt: string): ReadonlyArray { const lower = prompt.toLowerCase(); const matches: Skill[] = []; for (const skill of this.byId.values()) { const triggers = skill.frontmatter.triggers ?? []; if (triggers.some((t) => lower.includes(t.toLowerCase()))) matches.push(skill); } return matches; } /** * Register a skill. Throws on duplicate id — use `replace()` for * overwrite (or `replaceAll()` for atomic bulk swap). */ register(skill: Skill): void { if (this.byId.has(skill.id)) { throw new Error(`Skill already registered: ${skill.id}`); } this.warnNameCollision(skill.frontmatter.name, skill.id); this.byId.set(skill.id, skill); this.byNameIdx.set(skill.frontmatter.name, skill); } replace(skill: Skill): void { // Drop the prior definition's name from the index when it changed — // otherwise an edited skill re-registered under the same id but a new // frontmatter.name leaves the OLD name pointing at the stale skill // forever (two live names for one id). Mirrors CommandRegistry.replace's // alias cleanup. const prior = this.byId.get(skill.id); if (prior && prior.frontmatter.name !== skill.frontmatter.name) { this.byNameIdx.delete(prior.frontmatter.name); } this.byId.set(skill.id, skill); this.byNameIdx.set(skill.frontmatter.name, skill); } unregister(id: string): void { const skill = this.byId.get(id); if (!skill) return; this.byId.delete(id); this.byNameIdx.delete(skill.frontmatter.name); } clear(): void { this.byId.clear(); this.byNameIdx.clear(); } /** * Replace the registry's entire contents atomically (no observable empty * window). Used by `reload_skills` so a concurrent lookup never sees * zero skills while a rescan is in flight. */ replaceAll(skills: ReadonlyArray): void { const nextById = new Map(); const nextByName = new Map(); for (const s of skills) { nextById.set(s.id, s); const priorForName = nextByName.get(s.frontmatter.name); if (priorForName && priorForName.id !== s.id) { this.logger?.warn( 'SkillRegistry: two skills share a frontmatter name; last one wins by name', { name: s.frontmatter.name, existingId: priorForName.id, incomingId: s.id }, ); } nextByName.set(s.frontmatter.name, s); } // Synchronous swap: callers between these lines still see the OLD // registry (Maps aren't replaced, only repopulated). Clear+set is the // shortest synchronous window we can achieve in JS for Map fields. this.byId.clear(); this.byNameIdx.clear(); for (const [k, v] of nextById) this.byId.set(k, v); for (const [k, v] of nextByName) this.byNameIdx.set(k, v); } }