skill-autoroute 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -192,14 +192,14 @@ a known answer, 5 of which must get no hint.
192
192
  | ------------------------------------------ | --------- | --------------- | -------------------- | ---------------------- | ----------- |
193
193
  | 0.2.0 (haiku) | 35.5% | 50.0% | 22 / 40 | 12.9 | 10/13 |
194
194
  | Broad single-word rules (haiku) | 21.0% | 59.1% | 26 / 98 | 24.1 | 9/13 |
195
- | **0.3.0: task word plus object word (sonnet)** | **59.1%** | **59.1%** | **26 / 18** | **10.8** | **12/13** |
195
+ | **0.3.0 and later: task word plus object word (sonnet)** | **59.1%** | **59.1%** | **26 / 18** | **10.8** | **12/13** |
196
196
 
197
197
 
198
198
  Relative recall is the share of all useful hints that any version found. Broader rules doubled the hints but most of the
199
199
  new ones were wrong. Guessing the kind of work from the prompt (question, change, risky) to gate or add hints was also
200
200
  tested and made hints worse (F1 63.2 down to 55.7), so it is not used.
201
201
 
202
- Examples with 0.3.0:
202
+ Examples with 0.3.0 and later (0.4.0 added [your own trigger words](#your-own-trigger-words); the rules are the same):
203
203
 
204
204
  | Prompt | Hint |
205
205
  | ------------------------------------- | ----------------------- |
@@ -276,7 +276,8 @@ When your skills change, the hint line tells you so. Run `regenerate`.
276
276
 
277
277
  ![User-level and project-level tables; the hook merges both and the project wins on the same name](https://raw.githubusercontent.com/hanifhadyan/skill-autoroute/main/docs/img/flow-scopes.png)
278
278
 
279
- - **User level** works in every folder.
279
+ - **User level** works in every folder. It also picks up the skills that only one project loads (its own
280
+ `.claude/skills` and plugins enabled only in that project's settings) and hints them only inside that project.
280
281
  - **Project level** goes into the repository, so teammates get it on clone. It adds a hook to `.claude/settings.json`.
281
282
  Paths are stored relative to the project, and entries that point outside the project are skipped.
282
283
  - **Codex** has a user-level hook only. That hook still reads the project table of the folder you run Codex in.
package/hook/route.js CHANGED
@@ -157,12 +157,32 @@ export function staleCount(table, fs = FS) {
157
157
  // Names come from skill files and go into model context: short, plain characters only.
158
158
  const shownName = (v) => v.slice(0, MAX_NAME).replace(/[^A-Za-z0-9._:@/ -]/g, '?');
159
159
 
160
- function pick(prompt, table, { tool, fs = FS } = {}) {
160
+ // Where a prompt runs: the base folder and, for a git worktree, its main checkout (from the `gitdir:` line of base/.git).
161
+ function places(base, fs) {
162
+ if (typeof base !== 'string' || !base) return [];
163
+ const out = [fs.real(base)];
164
+ try {
165
+ const m = /^gitdir:\s*(.+?)\s*$/m.exec(readFileSync(join(base, '.git'), 'utf8'));
166
+ const i = m ? m[1].lastIndexOf(`${sep}.git${sep}worktrees${sep}`) : -1;
167
+ if (i > 0) out.push(fs.real(m[1].slice(0, i)));
168
+ } catch { /* not a worktree */ }
169
+ return out;
170
+ }
171
+
172
+ // An entry with `projects` belongs to those project folders only; without a base it is never hinted.
173
+ const inProject = (s, here, fs) => !Array.isArray(s.projects) || s.projects.some((p) => {
174
+ if (typeof p !== 'string' || !p) return false;
175
+ const root = fs.real(p);
176
+ return here.some((h) => h === root || h.startsWith(root + sep));
177
+ });
178
+
179
+ function pick(prompt, table, { tool, base, fs = FS } = {}) {
161
180
  const text = String(prompt).slice(0, MAX_PROMPT);
181
+ const here = places(base, fs);
162
182
  const ok = (v) => typeof v === 'string' && v !== '';
163
183
  const seen = new Set();
164
184
  const hits = table.skills
165
- .filter((s) => s && s.enabled && ok(s.name) && ok(s.path) && /[\\/]SKILL\.md$/.test(s.path) && (!tool || (Array.isArray(s.tools) ? s.tools : [s.tool]).includes(tool)))
185
+ .filter((s) => s && s.enabled && ok(s.name) && ok(s.path) && /[\\/]SKILL\.md$/.test(s.path) && (!tool || (Array.isArray(s.tools) ? s.tools : [s.tool]).includes(tool)) && inProject(s, here, fs))
166
186
  .map((s) => ({ s, own: ownFits(s, text) }))
167
187
  .filter(({ s, own }) => (own || fits(s.patterns, text)) && fs.mtime(s.path) !== null)
168
188
  .sort((a, b) => (b.own - a.own) || ((a.s.priority ?? 1e9) - (b.s.priority ?? 1e9)))
@@ -242,7 +262,7 @@ function main(self) {
242
262
  const base = process.env.CLAUDE_PROJECT_DIR || process.cwd();
243
263
  const table = merge(load(join(udir, 'routes.json')), resolveProject(load(join(base, '.skill-autoroute', 'routes.json')), base));
244
264
  if (!table) return;
245
- const hit = pick(prompt, table, { tool: process.argv[2] });
265
+ const hit = pick(prompt, table, { tool: process.argv[2], base });
246
266
  if (!hit) return;
247
267
  const ctx = hit.out;
248
268
  process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: ctx } }) + '\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skill-autoroute",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Per-prompt skill router hook for Claude Code and Codex",
5
5
  "keywords": [
6
6
  "claude-code",
package/src/discover.js CHANGED
@@ -66,9 +66,10 @@ function walk(root, realDir, rel, seen, found, outside, skipped) {
66
66
  export function discover(dirs) {
67
67
  const skills = [];
68
68
  const done = new Map(); // real dir -> skill, so a folder shared by two tools lists both
69
- for (const { dir, tool, source } of dirs) {
69
+ for (const { dir, tool, source, projects, only } of dirs) {
70
70
  let names;
71
71
  try { names = readdirSync(dir).sort(); } catch { continue; }
72
+ if (Array.isArray(only)) names = names.filter((n) => only.includes(n));
72
73
  for (const folder of names) {
73
74
  let real;
74
75
  try {
@@ -78,6 +79,8 @@ export function discover(dirs) {
78
79
  if (done.has(real)) {
79
80
  const prev = done.get(real);
80
81
  if (prev && !prev.tools.includes(tool)) prev.tools.push(tool);
82
+ // A skill stays scoped only while every dir that lists it is scoped; one global listing makes it global.
83
+ if (prev?.projects) prev.projects = projects ? [...new Set([...prev.projects, ...projects])] : undefined;
81
84
  continue;
82
85
  }
83
86
  const skillPath = join(real, 'SKILL.md');
@@ -99,7 +102,7 @@ export function discover(dirs) {
99
102
  const skill = {
100
103
  name: fm.name || folder,
101
104
  description: fm.description || '',
102
- tool, tools: [tool], source, dir: real, skillPath,
105
+ tool, tools: [tool], source, ...(projects ? { projects: [...projects] } : {}), dir: real, skillPath,
103
106
  hash: `sha256:${h.digest('hex')}`,
104
107
  files: found.map((f) => f.real),
105
108
  outside: outside.sort(),
package/src/flow.js CHANGED
@@ -191,6 +191,7 @@ const entry = (r, gen, scope) => {
191
191
  const g = r.enabled ? gen.bySkill.get(r.skill.name) : null;
192
192
  return {
193
193
  name: r.skill.name, tool: r.skill.tool, tools: r.skill.tools ?? [r.skill.tool], path: keyOf(scope)(r.skill.skillPath), hash: r.skill.hash, enabled: r.enabled,
194
+ ...(r.skill.projects ? { projects: r.skill.projects } : {}),
194
195
  priority: g?.priority ?? 50, patterns: g?.rules ?? [],
195
196
  scan: { verdict: r.scan.verdict, partial: r.scan.partial, findings: tableFindings(scope, r.skill, r.scan.findings) }, duplicateOf: r.dup?.of ?? null,
196
197
  };
@@ -346,7 +347,9 @@ export async function regenerate(o, ui) {
346
347
  if (was(s).patterns?.length && (!g || gen.fellBack.includes(s.name))) continue;
347
348
  byPath.set(s.skillPath, { ...was(s), priority: g?.priority ?? 50, patterns: g?.rules ?? [] });
348
349
  }
349
- const table = tableOf(ctx, skills.map((s) => byPath.get(s.skillPath) ?? was(s)), roots.map(key), old.log);
350
+ // Scope comes from this discovery, not the old entry: enabling a plugin in another project changes no skill file.
351
+ const scoped = ({ projects, ...e }, s) => (s.projects ? { ...e, projects: s.projects } : e);
352
+ const table = tableOf(ctx, skills.map((s) => scoped(byPath.get(s.skillPath) ?? was(s), s)), roots.map(key), old.log);
350
353
  keepWords(table, old);
351
354
  // unchanged rules still come from the old generator; after a full rebuild the new model wrote them all
352
355
  if (!o.ai && old.generator) table.generator = { ...old.generator, rules: RULES_VERSION, ...(outdated && ctx.ai ? { model: table.generator.model } : {}) };
@@ -1,6 +1,6 @@
1
1
  import { readFileSync, existsSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
- import { join } from 'node:path';
3
+ import { join, resolve, dirname, basename, sep } from 'node:path';
4
4
  import { addHook, removeHook as removeHookFrom, hasHook as hasHookIn } from './hookconfig.js';
5
5
  import { runCli, parseJson, onPath, checkPrompt } from './run.js';
6
6
 
@@ -20,8 +20,46 @@ function pluginDirs(settingsFile) {
20
20
  for (const [key, on] of Object.entries(enabled)) {
21
21
  if (on !== true || !Array.isArray(installed[key])) continue;
22
22
  for (const e of installed[key]) {
23
- const dir = typeof e?.installPath === 'string' && join(e.installPath, 'skills');
24
- if (dir && existsSync(dir)) out.push({ dir, tool: 'claude', source: `plugin:${key}` });
23
+ if (typeof e?.installPath !== 'string') continue;
24
+ const source = `plugin:${key}`;
25
+ const listed = readJson(join(e.installPath, '.claude-plugin', 'plugin.json'))?.skills;
26
+ if (!Array.isArray(listed)) {
27
+ const dir = join(e.installPath, 'skills');
28
+ if (existsSync(dir)) out.push({ dir, tool: 'claude', source });
29
+ continue;
30
+ }
31
+ // A manifest names its skill folders, which may sit deeper than skills/<name>: scan each parent for those names only.
32
+ const byParent = new Map();
33
+ for (const rel of listed) {
34
+ const p = typeof rel === 'string' && resolve(e.installPath, rel);
35
+ if (!p || !p.startsWith(e.installPath + sep) || !existsSync(p)) continue;
36
+ byParent.set(dirname(p), [...(byParent.get(dirname(p)) ?? []), basename(p)]);
37
+ }
38
+ for (const [dir, only] of byParent) out.push({ dir, tool: 'claude', source, only });
39
+ }
40
+ }
41
+ return out;
42
+ }
43
+
44
+ // Projects Claude Code has opened (~/.claude.json) or installed a plugin for (projectPath in installed_plugins.json). Their own skills and the plugins only they enable live outside
45
+ // the user dirs; the user table lists them with `projects`, and the hook hints them only inside that project.
46
+ function projectDirs(userDirs) {
47
+ const seen = new Set(userDirs.map((d) => d.dir));
48
+ const out = [];
49
+ const installed = Object.values(readJson(join(homedir(), '.claude', 'plugins', 'installed_plugins.json'))?.plugins ?? {});
50
+ const roots = new Set([
51
+ ...Object.keys(readJson(join(homedir(), '.claude.json'))?.projects ?? {}),
52
+ ...installed.flatMap((list) => (Array.isArray(list) ? list : [])).map((e) => e?.projectPath).filter((p) => typeof p === 'string'),
53
+ ]);
54
+ for (const root of roots) {
55
+ if (root === homedir() || !existsSync(root)) continue;
56
+ const projects = [root];
57
+ const own = join(root, '.claude', 'skills');
58
+ if (existsSync(own)) out.push({ dir: own, tool: 'claude', source: 'project', projects });
59
+ for (const f of ['settings.json', 'settings.local.json']) {
60
+ for (const d of pluginDirs(join(root, '.claude', f))) {
61
+ if (!seen.has(d.dir) && !out.some((o) => o.dir === d.dir && o.projects === projects)) out.push({ ...d, projects });
62
+ }
25
63
  }
26
64
  }
27
65
  return out;
@@ -29,8 +67,9 @@ function pluginDirs(settingsFile) {
29
67
 
30
68
  // Each scope lists only its own dirs; the hook merges the user and project tables at run time.
31
69
  export function skillDirs(scope) {
32
- const base = scope === 'project' ? join(process.cwd(), '.claude') : join(homedir(), '.claude');
33
- return [{ dir: join(base, 'skills'), tool: 'claude', source: scope === 'project' ? 'project' : 'user' }, ...pluginDirs(settings(scope))];
70
+ if (scope === 'project') return [{ dir: join(process.cwd(), '.claude', 'skills'), tool: 'claude', source: 'project' }, ...pluginDirs(settings(scope))];
71
+ const user = [{ dir: join(homedir(), '.claude', 'skills'), tool: 'claude', source: 'user' }, ...pluginDirs(settings(scope))];
72
+ return [...user, ...projectDirs(user)];
34
73
  }
35
74
 
36
75
  export const installHook = (scope, command) => addHook(settings(scope), command);