skill-autoroute 0.5.0 → 0.6.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 +16 -1
- package/hook/route.js +54 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,6 +45,19 @@ No match means no output and **0 extra tokens**. No AI runs at prompt time. The
|
|
|
45
45
|
|
|
46
46
|

|
|
47
47
|
|
|
48
|
+
### 3 · During the work (agent loops)
|
|
49
|
+
|
|
50
|
+
The hook fires only when a prompt arrives, not on every step of an agent loop. So the first prompt of each session
|
|
51
|
+
also tells the agent how to ask for skills itself before a new sub-task:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
node ~/.config/skill-autoroute/route.mjs claude suggest <session-id> "write the migration for the orders table"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The hook remembers which skills it already named in a session (in `~/.config/skill-autoroute/sessions/`, pruned
|
|
58
|
+
after 7 days). A later prompt, or a resumed round of a headless run (`claude -p --resume`), does not hint them again.
|
|
59
|
+
`suggest` always answers and adds what it names to that memory.
|
|
60
|
+
|
|
48
61
|
---
|
|
49
62
|
|
|
50
63
|
## Quick start
|
|
@@ -151,8 +164,10 @@ The router **adds** tokens. It does not save them. It pays off when a hint makes
|
|
|
151
164
|
|
|
152
165
|
| When | Cost |
|
|
153
166
|
| ------------------------------------------ | -------------------------------------------------------------- |
|
|
154
|
-
| A prompt matches nothing | **0 tokens**
|
|
167
|
+
| A prompt matches nothing | **0 tokens** (the first prompt of a session: the `suggest` line, about 40 tokens) |
|
|
155
168
|
| A prompt matches | one line, about 50–70 tokens per skill, at most 3 skills |
|
|
169
|
+
| A skill already named in this session | **0 tokens**: not hinted again |
|
|
170
|
+
| The agent runs `suggest` | one tool call plus the same hint line |
|
|
156
171
|
| Install (example: 7 skills, `--ai claude`) | 58,350 input tokens against an estimate of 58,200, in 11 calls |
|
|
157
172
|
|
|
158
173
|
|
package/hook/route.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Per-prompt hook for Claude Code and Codex. Node built-ins only; any error means no output, exit 0.
|
|
2
2
|
// argv[2] names the tool (claude or codex): only that tool's skills are hinted; no argument hints all.
|
|
3
|
-
import { readFileSync, statSync, readdirSync, realpathSync, appendFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
3
|
+
import { readFileSync, statSync, readdirSync, realpathSync, appendFileSync, mkdirSync, existsSync, unlinkSync } from 'node:fs';
|
|
4
4
|
import { join, dirname, resolve, sep } from 'node:path';
|
|
5
5
|
import { homedir } from 'node:os';
|
|
6
6
|
import { pathToFileURL } from 'node:url';
|
|
@@ -176,7 +176,7 @@ const inProject = (s, here, fs) => !Array.isArray(s.projects) || s.projects.some
|
|
|
176
176
|
return here.some((h) => h === root || h.startsWith(root + sep));
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
-
function pick(prompt, table, { tool, base, fs = FS } = {}) {
|
|
179
|
+
function pick(prompt, table, { tool, base, skip, fs = FS } = {}) {
|
|
180
180
|
const text = String(prompt).slice(0, MAX_PROMPT);
|
|
181
181
|
const here = places(base, fs);
|
|
182
182
|
const ok = (v) => typeof v === 'string' && v !== '';
|
|
@@ -187,14 +187,14 @@ function pick(prompt, table, { tool, base, fs = FS } = {}) {
|
|
|
187
187
|
.filter(({ s, own }) => (own || fits(s.patterns, text)) && fs.mtime(s.path) !== null)
|
|
188
188
|
.sort((a, b) => (b.own - a.own) || ((a.s.priority ?? 1e9) - (b.s.priority ?? 1e9)))
|
|
189
189
|
.map(({ s }) => s)
|
|
190
|
-
.filter((s) => !seen.has(s.name) && seen.add(s.name))
|
|
190
|
+
.filter((s) => !seen.has(s.name) && seen.add(s.name) && !skip?.has(s.name))
|
|
191
191
|
.slice(0, MAX_HINTS);
|
|
192
192
|
if (!hits.length) return null;
|
|
193
193
|
const safe = (v) => v.replace(/[\u0000-\u001f\u007f-\u009f]/g, '');
|
|
194
194
|
let out = 'Skills that fit this work (read each SKILL.md before you start): ' + hits.map((s) => `${shownName(s.name)} (${safe(s.path)})`).join('; ') + '.';
|
|
195
195
|
const n = staleCount(table, fs);
|
|
196
196
|
if (n) out += `\n${n} skill${n === 1 ? '' : 's'} changed since the last generate. Run: npx skill-autoroute regenerate`;
|
|
197
|
-
return { out, count: hits.length };
|
|
197
|
+
return { out, count: hits.length, names: hits.map((s) => s.name) };
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
export const route = (prompt, table, opts) => pick(prompt, table, opts)?.out ?? null;
|
|
@@ -250,26 +250,70 @@ const userClaudeHook = (udir) => {
|
|
|
250
250
|
} catch { return false; }
|
|
251
251
|
};
|
|
252
252
|
|
|
253
|
+
// Skills already named in a session, one name per line, so a resumed or repeated prompt does not hint (and the agent
|
|
254
|
+
// does not re-read) them again. An id that is not a plain token gets no memory: it is never used as a file name.
|
|
255
|
+
function sessionFile(udir, tool, sid) {
|
|
256
|
+
return typeof sid === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(sid) ? join(udir, 'sessions', `${tool || 'any'}-${sid}`) : null;
|
|
257
|
+
}
|
|
258
|
+
const remembered = (file) => {
|
|
259
|
+
try { return new Set(readFileSync(file, 'utf8').split('\n').filter(Boolean)); } catch { return null; }
|
|
260
|
+
};
|
|
261
|
+
function remember(file, names) {
|
|
262
|
+
if (!file) return;
|
|
263
|
+
try {
|
|
264
|
+
const dir = dirname(file);
|
|
265
|
+
if (!existsSync(file)) {
|
|
266
|
+
mkdirSync(dir, { recursive: true });
|
|
267
|
+
// shortcut: prune old sessions only when a new one starts; fine for a few hundred sessions a week.
|
|
268
|
+
for (const f of readdirSync(dir)) if ((FS.mtime(join(dir, f)) ?? Infinity) < Date.now() - 7 * 864e5) unlinkSync(join(dir, f));
|
|
269
|
+
}
|
|
270
|
+
appendFileSync(file, names.map((n) => `${n}\n`).join(''));
|
|
271
|
+
} catch { /* a read-only sandbox only loses the memory */ }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const loadTable = (udir, base) => merge(load(join(udir, 'routes.json')), resolveProject(load(join(base, '.skill-autoroute', 'routes.json')), base));
|
|
275
|
+
|
|
276
|
+
// `route.mjs [tool] suggest <session|-> <text...>`: the agent asks for skills in the middle of its work.
|
|
277
|
+
function suggest(udir, args, i) {
|
|
278
|
+
const tool = i === 1 ? args[0] : undefined;
|
|
279
|
+
const base = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
280
|
+
const table = loadTable(udir, base);
|
|
281
|
+
const hit = table && pick(args.slice(i + 2).join(' '), table, { tool, base });
|
|
282
|
+
process.stdout.write(`${hit ? hit.out : 'No skill fits.'}\n`);
|
|
283
|
+
if (hit) remember(sessionFile(udir, tool, args[i + 1]), hit.names);
|
|
284
|
+
}
|
|
285
|
+
|
|
253
286
|
function main(self) {
|
|
254
287
|
if (process.env.SKILL_AUTOROUTE_DISABLE === '1') return;
|
|
255
288
|
const udir = join(homedir(), '.config', 'skill-autoroute');
|
|
289
|
+
const args = process.argv.slice(2);
|
|
290
|
+
const at = args.indexOf('suggest');
|
|
291
|
+
if (at === 0 || at === 1) return suggest(udir, args, at);
|
|
256
292
|
// The user Claude hook already merges the project table; the project copy beside it would hint twice.
|
|
257
293
|
// Only a Claude user hook counts: a Codex-only user install must not silence the Claude project hook.
|
|
258
294
|
if (self.endsWith(`${sep}.skill-autoroute${sep}route.mjs`) && userClaudeHook(udir)) return;
|
|
259
295
|
try {
|
|
260
|
-
const { prompt } = JSON.parse(readFileSync(0, 'utf8'));
|
|
296
|
+
const { prompt, session_id: sid } = JSON.parse(readFileSync(0, 'utf8'));
|
|
261
297
|
if (typeof prompt !== 'string') return;
|
|
262
298
|
const base = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
263
|
-
const table =
|
|
299
|
+
const table = loadTable(udir, base);
|
|
264
300
|
if (!table) return;
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
const
|
|
301
|
+
const tool = args[0];
|
|
302
|
+
const file = sessionFile(udir, tool, sid);
|
|
303
|
+
const known = file && remembered(file);
|
|
304
|
+
const hit = pick(prompt, table, { tool, base, skip: known ?? undefined });
|
|
305
|
+
// Told once per session: later prompts and resumed rounds already have this line in context.
|
|
306
|
+
const tell = file && !known
|
|
307
|
+
? `To find skills for a new sub-task later in this session, run: node ${JSON.stringify(self)}${tool ? ` ${tool}` : ''} suggest ${sid} "<what you will do next>"`
|
|
308
|
+
: '';
|
|
309
|
+
if (!hit && !tell) return;
|
|
310
|
+
remember(file, hit ? hit.names : []);
|
|
311
|
+
const ctx = [hit?.out, tell].filter(Boolean).join('\n');
|
|
268
312
|
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: ctx } }) + '\n');
|
|
269
313
|
if (process.env.SKILL_AUTOROUTE_LOG === '1' || table.log === true) {
|
|
270
314
|
try {
|
|
271
315
|
mkdirSync(udir, { recursive: true });
|
|
272
|
-
appendFileSync(join(udir, 'usage.log'), `${new Date().toISOString()}\t${hit
|
|
316
|
+
appendFileSync(join(udir, 'usage.log'), `${new Date().toISOString()}\t${hit?.count ?? 0}\t${ctx.length}\n`);
|
|
273
317
|
} catch { /* logging must never break a prompt */ }
|
|
274
318
|
}
|
|
275
319
|
} catch (e) {
|