@klhapp/skillmux 1.9.3 → 1.11.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +19 -19
  3. package/docs/README.md +4 -4
  4. package/docs/assets/architecture-dark.svg +39 -32
  5. package/docs/assets/architecture-light.svg +25 -18
  6. package/docs/cli.md +147 -36
  7. package/docs/concepts.md +11 -11
  8. package/docs/configuration.md +7 -5
  9. package/docs/deployment.md +10 -6
  10. package/docs/getting-started.md +18 -14
  11. package/docs/mcp-routing.md +1 -1
  12. package/docs/skill-management.md +17 -11
  13. package/docs/troubleshooting.md +4 -4
  14. package/package.json +1 -1
  15. package/src/adapters.ts +157 -11
  16. package/src/cli.ts +396 -1319
  17. package/src/commands/audit.ts +53 -56
  18. package/src/commands/config.ts +33 -26
  19. package/src/commands/context.ts +104 -0
  20. package/src/commands/core.ts +7 -3
  21. package/src/commands/doctor.ts +97 -0
  22. package/src/commands/eval.ts +22 -15
  23. package/src/commands/init.ts +672 -0
  24. package/src/commands/install.ts +132 -0
  25. package/src/commands/local-vault.ts +60 -0
  26. package/src/commands/models.ts +10 -0
  27. package/src/commands/outdated.ts +2 -1
  28. package/src/commands/project.ts +194 -51
  29. package/src/commands/report.ts +66 -0
  30. package/src/commands/scan.ts +61 -0
  31. package/src/commands/shared.ts +7 -14
  32. package/src/commands/skill.ts +33 -0
  33. package/src/commands/sync.ts +232 -0
  34. package/src/commands/target.ts +45 -15
  35. package/src/commands/update.ts +2 -1
  36. package/src/completions.ts +41 -15
  37. package/src/config-service.ts +4 -54
  38. package/src/context.ts +8 -3
  39. package/src/db-audit.ts +286 -0
  40. package/src/db-index.ts +238 -0
  41. package/src/db.ts +3 -521
  42. package/src/global-flags.ts +46 -0
  43. package/src/init-agents.ts +329 -0
  44. package/src/init-instructions.ts +47 -28
  45. package/src/logger.ts +26 -0
  46. package/src/mcp-registration.ts +89 -0
  47. package/src/output.ts +80 -18
  48. package/src/prompts.ts +75 -20
  49. package/src/router-core.ts +8 -27
  50. package/src/scan.ts +19 -19
  51. package/src/server.ts +161 -14
  52. package/src/toml-writer.ts +51 -0
  53. package/src/init-clients.ts +0 -220
@@ -0,0 +1,238 @@
1
+ import { Database } from "bun:sqlite";
2
+ import { mkdirSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import type { VaultSkill } from "./vault";
5
+
6
+ export interface SkillRow {
7
+ skill_id: string;
8
+ title: string;
9
+ description: string;
10
+ aliases: string;
11
+ content_sha256: string;
12
+ }
13
+
14
+ export function openIndex(stateDir: string): Database {
15
+ mkdirSync(stateDir, { recursive: true });
16
+ const db = new Database(join(stateDir, "index.sqlite3"), { create: true });
17
+ db.run("PRAGMA journal_mode = WAL");
18
+ db.run("PRAGMA busy_timeout = 2000");
19
+ db.run(`CREATE TABLE IF NOT EXISTS skills (
20
+ skill_id TEXT PRIMARY KEY,
21
+ title TEXT NOT NULL,
22
+ description TEXT NOT NULL,
23
+ aliases TEXT NOT NULL,
24
+ content_sha256 TEXT NOT NULL
25
+ )`);
26
+ db.run(`CREATE VIRTUAL TABLE IF NOT EXISTS skills_fts USING fts5(
27
+ skill_id UNINDEXED, title, description, aliases,
28
+ tokenize = 'unicode61 remove_diacritics 2'
29
+ )`);
30
+ db.run(`CREATE TABLE IF NOT EXISTS vectors (
31
+ skill_id TEXT PRIMARY KEY,
32
+ content_sha256 TEXT NOT NULL,
33
+ embedding_fingerprint TEXT NOT NULL DEFAULT '',
34
+ dim INTEGER NOT NULL,
35
+ vec BLOB NOT NULL
36
+ )`);
37
+ const vectorColumns = db.query("PRAGMA table_info(vectors)").all() as { name: string }[];
38
+ if (!vectorColumns.some((column) => column.name === "embedding_fingerprint")) {
39
+ db.run("ALTER TABLE vectors ADD COLUMN embedding_fingerprint TEXT NOT NULL DEFAULT ''");
40
+ }
41
+ // Audit rows live in audit.sqlite3; openAudit adopts any left here.
42
+ db.run(`CREATE TABLE IF NOT EXISTS index_meta (
43
+ key TEXT PRIMARY KEY,
44
+ value TEXT NOT NULL
45
+ )`);
46
+ return db;
47
+ }
48
+
49
+ export function upsertSkill(db: Database, skill: VaultSkill): void {
50
+ const aliases = skill.aliases.join(" ");
51
+ db.transaction(() => {
52
+ db.run("DELETE FROM skills WHERE skill_id = ?", [skill.skill_id]);
53
+ db.run("DELETE FROM skills_fts WHERE skill_id = ?", [skill.skill_id]);
54
+ db.run(
55
+ "INSERT INTO skills (skill_id, title, description, aliases, content_sha256) VALUES (?, ?, ?, ?, ?)",
56
+ [skill.skill_id, skill.title, skill.description, aliases, skill.content_sha256],
57
+ );
58
+ db.run(
59
+ "INSERT INTO skills_fts (skill_id, title, description, aliases) VALUES (?, ?, ?, ?)",
60
+ [skill.skill_id, skill.title, skill.description, aliases],
61
+ );
62
+ })();
63
+ }
64
+
65
+ export function toSkillRow(skill: VaultSkill): SkillRow {
66
+ return {
67
+ skill_id: skill.skill_id,
68
+ title: skill.title,
69
+ description: skill.description,
70
+ aliases: skill.aliases.join(" "),
71
+ content_sha256: skill.content_sha256,
72
+ };
73
+ }
74
+
75
+ /** Replace the whole lexical index with `rows`; drops vectors of removed skills. */
76
+ export function replaceSkills(db: Database, rows: SkillRow[]): void {
77
+ db.transaction(() => {
78
+ db.run("DELETE FROM skills");
79
+ db.run("DELETE FROM skills_fts");
80
+ for (const row of rows) {
81
+ db.run(
82
+ "INSERT INTO skills (skill_id, title, description, aliases, content_sha256) VALUES (?, ?, ?, ?, ?)",
83
+ [row.skill_id, row.title, row.description, row.aliases, row.content_sha256],
84
+ );
85
+ db.run(
86
+ "INSERT INTO skills_fts (skill_id, title, description, aliases) VALUES (?, ?, ?, ?)",
87
+ [row.skill_id, row.title, row.description, row.aliases],
88
+ );
89
+ }
90
+ db.run("DELETE FROM vectors WHERE skill_id NOT IN (SELECT skill_id FROM skills)");
91
+ })();
92
+ }
93
+
94
+ export function ingestVault(db: Database, skills: VaultSkill[]): void {
95
+ replaceSkills(db, skills.map(toSkillRow));
96
+ }
97
+
98
+ export function deleteSkill(db: Database, skillId: string): void {
99
+ db.transaction(() => {
100
+ db.run("DELETE FROM skills WHERE skill_id = ?", [skillId]);
101
+ db.run("DELETE FROM skills_fts WHERE skill_id = ?", [skillId]);
102
+ db.run("DELETE FROM vectors WHERE skill_id = ?", [skillId]);
103
+ })();
104
+ }
105
+
106
+ export function skillCount(db: Database): number {
107
+ return (db.query("SELECT count(*) AS n FROM skills").get() as { n: number }).n;
108
+ }
109
+
110
+ export function getSkillRow(db: Database, skillId: string): SkillRow | null {
111
+ return db.query("SELECT * FROM skills WHERE skill_id = ?").get(skillId) as SkillRow | null;
112
+ }
113
+
114
+ /**
115
+ * Sanitize free text into an FTS5 OR-query; returns null when no usable terms
116
+ * remain. Terms keep any Unicode letters/digits (CJK included) so non-ASCII
117
+ * queries still get lexical recall — unicode61 tokenizes contiguous CJK runs
118
+ * as single tokens, so matching works at that granularity.
119
+ */
120
+ export function toFtsQuery(text: string): string | null {
121
+ const terms = [
122
+ ...new Set(
123
+ text
124
+ .toLowerCase()
125
+ .split(/[^\p{L}\p{N}]+/u)
126
+ .filter((t) => t.length >= 2),
127
+ ),
128
+ ];
129
+ if (terms.length === 0) return null;
130
+ return terms.map((t) => `"${t}"`).join(" OR ");
131
+ }
132
+
133
+ export function ftsSearch(db: Database, text: string, k: number): SkillRow[] {
134
+ const query = toFtsQuery(text);
135
+ if (query === null) return [];
136
+ return db
137
+ .query(
138
+ `SELECT s.* FROM skills_fts f
139
+ JOIN skills s ON s.skill_id = f.skill_id
140
+ WHERE skills_fts MATCH ?
141
+ ORDER BY bm25(skills_fts) LIMIT ?`,
142
+ )
143
+ .all(query, k) as SkillRow[];
144
+ }
145
+
146
+ export function findExactMatch(db: Database, query: string): SkillRow | null {
147
+ const cleanQuery = query.trim().toLowerCase();
148
+ return db
149
+ .query(
150
+ `SELECT * FROM skills
151
+ WHERE lower(skill_id) = ?
152
+ OR lower(title) = ?
153
+ OR ' ' || lower(aliases) || ' ' LIKE ?`,
154
+ )
155
+ .get(cleanQuery, cleanQuery, `% ${cleanQuery} %`) as SkillRow | null;
156
+ }
157
+
158
+ export function getIndexMeta(db: Database, key: string): string | null {
159
+ const row = db
160
+ .query("SELECT value FROM index_meta WHERE key = ?")
161
+ .get(key) as { value: string } | null;
162
+ return row ? row.value : null;
163
+ }
164
+
165
+ export function setIndexMeta(db: Database, key: string, value: string): void {
166
+ db.run(
167
+ `INSERT INTO index_meta (key, value) VALUES (?, ?)
168
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
169
+ [key, value],
170
+ );
171
+ }
172
+
173
+ export function upsertVector(
174
+ db: Database,
175
+ skillId: string,
176
+ contentSha256: string,
177
+ embeddingFingerprint: string,
178
+ vec: Float32Array,
179
+ ): void {
180
+ db.run(
181
+ `INSERT INTO vectors (skill_id, content_sha256, embedding_fingerprint, dim, vec) VALUES (?, ?, ?, ?, ?)
182
+ ON CONFLICT(skill_id) DO UPDATE SET
183
+ content_sha256 = excluded.content_sha256,
184
+ embedding_fingerprint = excluded.embedding_fingerprint,
185
+ dim = excluded.dim,
186
+ vec = excluded.vec`,
187
+ [skillId, contentSha256, embeddingFingerprint, vec.length, new Uint8Array(vec.buffer, vec.byteOffset, vec.byteLength)],
188
+ );
189
+ }
190
+
191
+ /**
192
+ * Skills with no usable stored vector: none at all, content changed since
193
+ * embedding, or embedded at a different dimension than currently configured.
194
+ */
195
+ export function skillsNeedingVectors(db: Database, dimension: number, embeddingFingerprint: string): SkillRow[] {
196
+ return db
197
+ .query(
198
+ `SELECT s.* FROM skills s
199
+ LEFT JOIN vectors v ON v.skill_id = s.skill_id
200
+ AND v.content_sha256 = s.content_sha256
201
+ AND v.dim = ?
202
+ AND v.embedding_fingerprint = ?
203
+ WHERE v.skill_id IS NULL`,
204
+ )
205
+ .all(dimension, embeddingFingerprint) as SkillRow[];
206
+ }
207
+
208
+ function cosine(a: Float32Array, b: Float32Array): number {
209
+ if (a.length !== b.length) return 0;
210
+ let dot = 0;
211
+ let normA = 0;
212
+ let normB = 0;
213
+ for (let i = 0; i < a.length; i++) {
214
+ dot += a[i]! * b[i]!;
215
+ normA += a[i]! * a[i]!;
216
+ normB += b[i]! * b[i]!;
217
+ }
218
+ if (normA === 0 || normB === 0) return 0;
219
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
220
+ }
221
+
222
+ /** Brute-force cosine over every stored vector (vault is ~100 skills; no ANN). */
223
+ export function vectorTopK(db: Database, query: Float32Array, k: number): SkillRow[] {
224
+ const rows = db
225
+ .query(
226
+ `SELECT s.skill_id, s.title, s.description, s.aliases, s.content_sha256, v.vec
227
+ FROM vectors v JOIN skills s ON s.skill_id = v.skill_id`,
228
+ )
229
+ .all() as (SkillRow & { vec: Uint8Array })[];
230
+ return rows
231
+ .map(({ vec, ...row }) => ({
232
+ row,
233
+ score: cosine(query, new Float32Array(vec.buffer, vec.byteOffset, vec.byteLength / 4)),
234
+ }))
235
+ .sort((a, b) => b.score - a.score)
236
+ .slice(0, k)
237
+ .map((r) => r.row);
238
+ }