agentikit 0.0.8 → 0.0.12
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 +135 -117
- package/dist/index.d.ts +13 -3
- package/dist/index.js +7 -1
- package/dist/src/asset-spec.d.ts +2 -0
- package/dist/src/asset-spec.js +22 -3
- package/dist/src/asset-type-handler.d.ts +27 -0
- package/dist/src/asset-type-handler.js +33 -0
- package/dist/src/cli.js +335 -100
- package/dist/src/common.d.ts +6 -1
- package/dist/src/common.js +18 -4
- package/dist/src/config-cli.d.ts +9 -0
- package/dist/src/config-cli.js +473 -0
- package/dist/src/config.d.ts +25 -6
- package/dist/src/config.js +188 -28
- package/dist/src/db.d.ts +46 -0
- package/dist/src/db.js +299 -0
- package/dist/src/embedder.js +12 -7
- package/dist/src/github.d.ts +4 -0
- package/dist/src/github.js +19 -0
- package/dist/src/handlers/agent-handler.d.ts +2 -0
- package/dist/src/handlers/agent-handler.js +26 -0
- package/dist/src/handlers/command-handler.d.ts +2 -0
- package/dist/src/handlers/command-handler.js +23 -0
- package/dist/src/handlers/index.d.ts +6 -0
- package/dist/src/handlers/index.js +23 -0
- package/dist/src/handlers/knowledge-handler.d.ts +2 -0
- package/dist/src/handlers/knowledge-handler.js +56 -0
- package/dist/src/handlers/markdown-helpers.d.ts +7 -0
- package/dist/src/handlers/markdown-helpers.js +15 -0
- package/dist/src/handlers/script-handler.d.ts +2 -0
- package/dist/src/handlers/script-handler.js +78 -0
- package/dist/src/handlers/skill-handler.d.ts +2 -0
- package/dist/src/handlers/skill-handler.js +30 -0
- package/dist/src/handlers/tool-handler.d.ts +2 -0
- package/dist/src/handlers/tool-handler.js +58 -0
- package/dist/src/indexer.d.ts +1 -23
- package/dist/src/indexer.js +162 -155
- package/dist/src/init.d.ts +2 -2
- package/dist/src/init.js +21 -9
- package/dist/src/llm.js +4 -3
- package/dist/src/metadata.d.ts +1 -1
- package/dist/src/metadata.js +22 -64
- package/dist/src/origin-resolve.d.ts +19 -0
- package/dist/src/origin-resolve.js +53 -0
- package/dist/src/registry-install.d.ts +11 -0
- package/dist/src/registry-install.js +315 -0
- package/dist/src/registry-resolve.d.ts +3 -0
- package/dist/src/registry-resolve.js +299 -0
- package/dist/src/registry-search.d.ts +27 -0
- package/dist/src/registry-search.js +263 -0
- package/dist/src/registry-types.d.ts +62 -0
- package/dist/src/registry-types.js +1 -0
- package/dist/src/stash-add.d.ts +4 -0
- package/dist/src/stash-add.js +59 -0
- package/dist/src/stash-clone.d.ts +22 -0
- package/dist/src/stash-clone.js +83 -0
- package/dist/src/stash-ref.d.ts +27 -3
- package/dist/src/stash-ref.js +63 -24
- package/dist/src/stash-registry.d.ts +18 -0
- package/dist/src/stash-registry.js +221 -0
- package/dist/src/stash-resolve.js +3 -0
- package/dist/src/stash-search.d.ts +3 -1
- package/dist/src/stash-search.js +357 -138
- package/dist/src/stash-show.d.ts +1 -1
- package/dist/src/stash-show.js +28 -89
- package/dist/src/stash-source.d.ts +24 -0
- package/dist/src/stash-source.js +81 -0
- package/dist/src/stash-types.d.ts +175 -1
- package/dist/src/stash.d.ts +9 -1
- package/dist/src/stash.js +5 -0
- package/dist/src/tool-runner.d.ts +1 -1
- package/dist/src/tool-runner.js +18 -5
- package/package.json +7 -2
- package/src/asset-spec.ts +20 -4
- package/src/asset-type-handler.ts +77 -0
- package/src/cli.ts +354 -103
- package/src/common.ts +23 -5
- package/src/config-cli.ts +499 -0
- package/src/config.ts +218 -37
- package/src/db.ts +411 -0
- package/src/embedder.ts +22 -11
- package/src/github.ts +21 -0
- package/src/handlers/agent-handler.ts +32 -0
- package/src/handlers/command-handler.ts +29 -0
- package/src/handlers/index.ts +25 -0
- package/src/handlers/knowledge-handler.ts +62 -0
- package/src/handlers/markdown-helpers.ts +19 -0
- package/src/handlers/script-handler.ts +92 -0
- package/src/handlers/skill-handler.ts +37 -0
- package/src/handlers/tool-handler.ts +71 -0
- package/src/indexer.ts +208 -187
- package/src/init.ts +17 -9
- package/src/llm.ts +4 -3
- package/src/metadata.ts +21 -65
- package/src/origin-resolve.ts +67 -0
- package/src/registry-install.ts +361 -0
- package/src/registry-resolve.ts +341 -0
- package/src/registry-search.ts +335 -0
- package/src/registry-types.ts +72 -0
- package/src/stash-add.ts +63 -0
- package/src/stash-clone.ts +127 -0
- package/src/stash-ref.ts +84 -26
- package/src/stash-registry.ts +259 -0
- package/src/stash-resolve.ts +3 -0
- package/src/stash-search.ts +425 -155
- package/src/stash-show.ts +33 -82
- package/src/stash-source.ts +103 -0
- package/src/stash-types.ts +186 -1
- package/src/stash.ts +23 -0
- package/src/tool-runner.ts +18 -5
- package/dist/src/similarity.d.ts +0 -34
- package/src/similarity.ts +0 -271
package/src/db.ts
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { Database } from "bun:sqlite"
|
|
4
|
+
import type { StashEntry } from "./metadata"
|
|
5
|
+
import type { EmbeddingVector } from "./embedder"
|
|
6
|
+
|
|
7
|
+
// ── Types ───────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export interface DbIndexedEntry {
|
|
10
|
+
id: number
|
|
11
|
+
entryKey: string
|
|
12
|
+
dirPath: string
|
|
13
|
+
filePath: string
|
|
14
|
+
stashDir: string
|
|
15
|
+
entry: StashEntry
|
|
16
|
+
searchText: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface DbSearchResult {
|
|
20
|
+
id: number
|
|
21
|
+
filePath: string
|
|
22
|
+
entry: StashEntry
|
|
23
|
+
searchText: string
|
|
24
|
+
bm25Score: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DbVecResult {
|
|
28
|
+
id: number
|
|
29
|
+
distance: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ── Constants ───────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
export const DB_VERSION = 5
|
|
35
|
+
export const EMBEDDING_DIM = 384
|
|
36
|
+
|
|
37
|
+
// ── Path ────────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
export function getDbPath(): string {
|
|
40
|
+
const cacheDir =
|
|
41
|
+
process.env.XDG_CACHE_HOME ||
|
|
42
|
+
path.join(process.env.HOME || process.env.USERPROFILE || "", ".cache")
|
|
43
|
+
return path.join(cacheDir, "agentikit", "index.db")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ── Database lifecycle ──────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
export function openDatabase(dbPath?: string, options?: { embeddingDim?: number }): Database {
|
|
49
|
+
const resolvedPath = dbPath ?? getDbPath()
|
|
50
|
+
const dir = path.dirname(resolvedPath)
|
|
51
|
+
if (!fs.existsSync(dir)) {
|
|
52
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const db = new Database(resolvedPath)
|
|
56
|
+
db.exec("PRAGMA journal_mode = WAL")
|
|
57
|
+
db.exec("PRAGMA foreign_keys = ON")
|
|
58
|
+
|
|
59
|
+
// Try to load sqlite-vec extension
|
|
60
|
+
loadVecExtension(db)
|
|
61
|
+
|
|
62
|
+
ensureSchema(db, options?.embeddingDim ?? EMBEDDING_DIM)
|
|
63
|
+
return db
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function closeDatabase(db: Database): void {
|
|
67
|
+
db.close()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ── sqlite-vec extension ────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
let vecAvailable = false
|
|
73
|
+
|
|
74
|
+
function loadVecExtension(db: Database): void {
|
|
75
|
+
try {
|
|
76
|
+
const sqliteVec = require("sqlite-vec")
|
|
77
|
+
sqliteVec.load(db)
|
|
78
|
+
vecAvailable = true
|
|
79
|
+
} catch {
|
|
80
|
+
console.warn("sqlite-vec extension not available, embeddings will be skipped")
|
|
81
|
+
vecAvailable = false
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function isVecAvailable(): boolean {
|
|
86
|
+
return vecAvailable
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ── Schema ──────────────────────────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
function ensureSchema(db: Database, embeddingDim: number): void {
|
|
92
|
+
// Create meta table first so we can check version
|
|
93
|
+
db.exec(`
|
|
94
|
+
CREATE TABLE IF NOT EXISTS index_meta (
|
|
95
|
+
key TEXT PRIMARY KEY,
|
|
96
|
+
value TEXT NOT NULL
|
|
97
|
+
);
|
|
98
|
+
`)
|
|
99
|
+
|
|
100
|
+
// Check stored version — if it differs from DB_VERSION, drop and recreate all tables
|
|
101
|
+
const storedVersion = getMeta(db, "version")
|
|
102
|
+
if (storedVersion && storedVersion !== String(DB_VERSION)) {
|
|
103
|
+
db.exec("DROP TABLE IF EXISTS entries_vec")
|
|
104
|
+
db.exec("DROP TABLE IF EXISTS entries_fts")
|
|
105
|
+
db.exec("DROP INDEX IF EXISTS idx_entries_dir")
|
|
106
|
+
db.exec("DROP INDEX IF EXISTS idx_entries_type")
|
|
107
|
+
db.exec("DROP TABLE IF EXISTS entries")
|
|
108
|
+
db.exec("DELETE FROM index_meta")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
db.exec(`
|
|
112
|
+
CREATE TABLE IF NOT EXISTS entries (
|
|
113
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
114
|
+
entry_key TEXT NOT NULL UNIQUE,
|
|
115
|
+
dir_path TEXT NOT NULL,
|
|
116
|
+
file_path TEXT NOT NULL,
|
|
117
|
+
stash_dir TEXT NOT NULL,
|
|
118
|
+
entry_json TEXT NOT NULL,
|
|
119
|
+
search_text TEXT NOT NULL,
|
|
120
|
+
entry_type TEXT NOT NULL
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
CREATE INDEX IF NOT EXISTS idx_entries_dir ON entries(dir_path);
|
|
124
|
+
CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type);
|
|
125
|
+
`)
|
|
126
|
+
|
|
127
|
+
// FTS5 table — standalone with explicit entry_id for joining
|
|
128
|
+
const ftsExists = db
|
|
129
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='entries_fts'")
|
|
130
|
+
.get()
|
|
131
|
+
if (!ftsExists) {
|
|
132
|
+
db.exec(`
|
|
133
|
+
CREATE VIRTUAL TABLE entries_fts USING fts5(
|
|
134
|
+
entry_id UNINDEXED,
|
|
135
|
+
search_text,
|
|
136
|
+
tokenize='porter unicode61'
|
|
137
|
+
);
|
|
138
|
+
`)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// sqlite-vec table
|
|
142
|
+
if (vecAvailable) {
|
|
143
|
+
// Check if stored embedding dimension differs from configured one
|
|
144
|
+
const storedDim = getMeta(db, "embeddingDim")
|
|
145
|
+
if (storedDim && storedDim !== String(embeddingDim)) {
|
|
146
|
+
try { db.exec("DROP TABLE IF EXISTS entries_vec") } catch { /* ignore */ }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const vecExists = db
|
|
150
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='entries_vec'")
|
|
151
|
+
.get()
|
|
152
|
+
if (!vecExists) {
|
|
153
|
+
db.exec(`
|
|
154
|
+
CREATE VIRTUAL TABLE entries_vec USING vec0(
|
|
155
|
+
id INTEGER PRIMARY KEY,
|
|
156
|
+
embedding FLOAT[${embeddingDim}]
|
|
157
|
+
);
|
|
158
|
+
`)
|
|
159
|
+
}
|
|
160
|
+
setMeta(db, "embeddingDim", String(embeddingDim))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Set version if not present
|
|
164
|
+
const version = getMeta(db, "version")
|
|
165
|
+
if (!version) {
|
|
166
|
+
setMeta(db, "version", String(DB_VERSION))
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ── Meta helpers ────────────────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
export function getMeta(db: Database, key: string): string | undefined {
|
|
173
|
+
const row = db.prepare("SELECT value FROM index_meta WHERE key = ?").get(key) as
|
|
174
|
+
| { value: string }
|
|
175
|
+
| undefined
|
|
176
|
+
return row?.value
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function setMeta(db: Database, key: string, value: string): void {
|
|
180
|
+
db.prepare("INSERT OR REPLACE INTO index_meta (key, value) VALUES (?, ?)").run(key, value)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ── Entry operations ────────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
export function upsertEntry(
|
|
186
|
+
db: Database,
|
|
187
|
+
entryKey: string,
|
|
188
|
+
dirPath: string,
|
|
189
|
+
filePath: string,
|
|
190
|
+
stashDir: string,
|
|
191
|
+
entry: StashEntry,
|
|
192
|
+
searchText: string,
|
|
193
|
+
): number {
|
|
194
|
+
const stmt = db.prepare(`
|
|
195
|
+
INSERT INTO entries (entry_key, dir_path, file_path, stash_dir, entry_json, search_text, entry_type)
|
|
196
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
197
|
+
ON CONFLICT(entry_key) DO UPDATE SET
|
|
198
|
+
dir_path = excluded.dir_path,
|
|
199
|
+
file_path = excluded.file_path,
|
|
200
|
+
stash_dir = excluded.stash_dir,
|
|
201
|
+
entry_json = excluded.entry_json,
|
|
202
|
+
search_text = excluded.search_text,
|
|
203
|
+
entry_type = excluded.entry_type
|
|
204
|
+
`)
|
|
205
|
+
stmt.run(entryKey, dirPath, filePath, stashDir, JSON.stringify(entry), searchText, entry.type)
|
|
206
|
+
// Fetch the row id explicitly since last_insert_rowid() is unreliable for ON CONFLICT DO UPDATE
|
|
207
|
+
const row = db.prepare("SELECT id FROM entries WHERE entry_key = ?").get(entryKey) as { id: number }
|
|
208
|
+
return row.id
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function deleteEntriesByDir(db: Database, dirPath: string): void {
|
|
212
|
+
if (vecAvailable) {
|
|
213
|
+
const ids = db
|
|
214
|
+
.prepare("SELECT id FROM entries WHERE dir_path = ?")
|
|
215
|
+
.all(dirPath) as Array<{ id: number }>
|
|
216
|
+
for (const { id } of ids) {
|
|
217
|
+
try {
|
|
218
|
+
db.prepare("DELETE FROM entries_vec WHERE id = ?").run(id)
|
|
219
|
+
} catch { /* ignore if vec table missing */ }
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
db.prepare("DELETE FROM entries WHERE dir_path = ?").run(dirPath)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function rebuildFts(db: Database): void {
|
|
226
|
+
db.exec("DELETE FROM entries_fts")
|
|
227
|
+
db.exec("INSERT INTO entries_fts (entry_id, search_text) SELECT CAST(id AS TEXT), search_text FROM entries")
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ── Vector operations ───────────────────────────────────────────────────────
|
|
231
|
+
|
|
232
|
+
export function upsertEmbedding(
|
|
233
|
+
db: Database,
|
|
234
|
+
entryId: number,
|
|
235
|
+
embedding: EmbeddingVector,
|
|
236
|
+
): void {
|
|
237
|
+
if (!vecAvailable) return
|
|
238
|
+
const buf = float32Buffer(embedding)
|
|
239
|
+
try {
|
|
240
|
+
db.prepare("DELETE FROM entries_vec WHERE id = ?").run(entryId)
|
|
241
|
+
} catch { /* ignore */ }
|
|
242
|
+
db.prepare("INSERT INTO entries_vec (id, embedding) VALUES (?, ?)").run(entryId, buf)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function searchVec(
|
|
246
|
+
db: Database,
|
|
247
|
+
queryEmbedding: EmbeddingVector,
|
|
248
|
+
k: number,
|
|
249
|
+
): DbVecResult[] {
|
|
250
|
+
if (!vecAvailable) return []
|
|
251
|
+
const buf = float32Buffer(queryEmbedding)
|
|
252
|
+
try {
|
|
253
|
+
return db
|
|
254
|
+
.prepare("SELECT id, distance FROM entries_vec WHERE embedding MATCH ? AND k = ?")
|
|
255
|
+
.all(buf, k) as DbVecResult[]
|
|
256
|
+
} catch {
|
|
257
|
+
return []
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function float32Buffer(vec: number[]): Buffer {
|
|
262
|
+
const f32 = new Float32Array(vec)
|
|
263
|
+
return Buffer.from(f32.buffer)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ── FTS5 search ─────────────────────────────────────────────────────────────
|
|
267
|
+
|
|
268
|
+
export function searchFts(
|
|
269
|
+
db: Database,
|
|
270
|
+
query: string,
|
|
271
|
+
limit: number,
|
|
272
|
+
entryType?: string,
|
|
273
|
+
): DbSearchResult[] {
|
|
274
|
+
const ftsQuery = sanitizeFtsQuery(query)
|
|
275
|
+
if (!ftsQuery) return []
|
|
276
|
+
|
|
277
|
+
let sql: string
|
|
278
|
+
let params: unknown[]
|
|
279
|
+
|
|
280
|
+
if (entryType && entryType !== "any") {
|
|
281
|
+
sql = `
|
|
282
|
+
SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
|
|
283
|
+
bm25(entries_fts) AS bm25Score
|
|
284
|
+
FROM entries_fts f
|
|
285
|
+
JOIN entries e ON e.id = CAST(f.entry_id AS INTEGER)
|
|
286
|
+
WHERE entries_fts MATCH ?
|
|
287
|
+
AND e.entry_type = ?
|
|
288
|
+
ORDER BY bm25Score
|
|
289
|
+
LIMIT ?
|
|
290
|
+
`
|
|
291
|
+
params = [ftsQuery, entryType, limit]
|
|
292
|
+
} else {
|
|
293
|
+
sql = `
|
|
294
|
+
SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
|
|
295
|
+
bm25(entries_fts) AS bm25Score
|
|
296
|
+
FROM entries_fts f
|
|
297
|
+
JOIN entries e ON e.id = CAST(f.entry_id AS INTEGER)
|
|
298
|
+
WHERE entries_fts MATCH ?
|
|
299
|
+
ORDER BY bm25Score
|
|
300
|
+
LIMIT ?
|
|
301
|
+
`
|
|
302
|
+
params = [ftsQuery, limit]
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
const rows = db.prepare(sql).all(...(params as import("bun:sqlite").SQLQueryBindings[])) as Array<{
|
|
307
|
+
id: number
|
|
308
|
+
filePath: string
|
|
309
|
+
entry_json: string
|
|
310
|
+
searchText: string
|
|
311
|
+
bm25Score: number
|
|
312
|
+
}>
|
|
313
|
+
|
|
314
|
+
return rows.map((row) => ({
|
|
315
|
+
id: row.id,
|
|
316
|
+
filePath: row.filePath,
|
|
317
|
+
entry: JSON.parse(row.entry_json) as StashEntry,
|
|
318
|
+
searchText: row.searchText,
|
|
319
|
+
bm25Score: row.bm25Score,
|
|
320
|
+
}))
|
|
321
|
+
} catch {
|
|
322
|
+
return []
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function sanitizeFtsQuery(query: string): string {
|
|
327
|
+
const tokens = query
|
|
328
|
+
.replace(/[^a-zA-Z0-9\s]/g, " ")
|
|
329
|
+
.split(/\s+/)
|
|
330
|
+
.filter((t) => t.length > 1)
|
|
331
|
+
if (tokens.length === 0) return ""
|
|
332
|
+
// Use unquoted tokens so the porter stemmer can normalize word forms
|
|
333
|
+
return tokens.join(" OR ")
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ── All entries ─────────────────────────────────────────────────────────────
|
|
337
|
+
|
|
338
|
+
export function getAllEntries(
|
|
339
|
+
db: Database,
|
|
340
|
+
entryType?: string,
|
|
341
|
+
): DbIndexedEntry[] {
|
|
342
|
+
let sql: string
|
|
343
|
+
let params: unknown[]
|
|
344
|
+
|
|
345
|
+
if (entryType && entryType !== "any") {
|
|
346
|
+
sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE entry_type = ?"
|
|
347
|
+
params = [entryType]
|
|
348
|
+
} else {
|
|
349
|
+
sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries"
|
|
350
|
+
params = []
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const rows = db.prepare(sql).all(...(params as import("bun:sqlite").SQLQueryBindings[])) as Array<{
|
|
354
|
+
id: number
|
|
355
|
+
entry_key: string
|
|
356
|
+
dir_path: string
|
|
357
|
+
file_path: string
|
|
358
|
+
stash_dir: string
|
|
359
|
+
entry_json: string
|
|
360
|
+
search_text: string
|
|
361
|
+
}>
|
|
362
|
+
|
|
363
|
+
return rows.map((row) => ({
|
|
364
|
+
id: row.id,
|
|
365
|
+
entryKey: row.entry_key,
|
|
366
|
+
dirPath: row.dir_path,
|
|
367
|
+
filePath: row.file_path,
|
|
368
|
+
stashDir: row.stash_dir,
|
|
369
|
+
entry: JSON.parse(row.entry_json) as StashEntry,
|
|
370
|
+
searchText: row.search_text,
|
|
371
|
+
}))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function getEntryCount(db: Database): number {
|
|
375
|
+
const row = db.prepare("SELECT COUNT(*) AS cnt FROM entries").get() as { cnt: number }
|
|
376
|
+
return row.cnt
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function getEntryById(db: Database, id: number): { filePath: string; entry: StashEntry } | undefined {
|
|
380
|
+
const row = db
|
|
381
|
+
.prepare("SELECT file_path, entry_json FROM entries WHERE id = ?")
|
|
382
|
+
.get(id) as { file_path: string; entry_json: string } | undefined
|
|
383
|
+
if (!row) return undefined
|
|
384
|
+
return { filePath: row.file_path, entry: JSON.parse(row.entry_json) as StashEntry }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function getEntriesByDir(db: Database, dirPath: string): DbIndexedEntry[] {
|
|
388
|
+
const rows = db
|
|
389
|
+
.prepare(
|
|
390
|
+
"SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE dir_path = ?",
|
|
391
|
+
)
|
|
392
|
+
.all(dirPath) as Array<{
|
|
393
|
+
id: number
|
|
394
|
+
entry_key: string
|
|
395
|
+
dir_path: string
|
|
396
|
+
file_path: string
|
|
397
|
+
stash_dir: string
|
|
398
|
+
entry_json: string
|
|
399
|
+
search_text: string
|
|
400
|
+
}>
|
|
401
|
+
|
|
402
|
+
return rows.map((row) => ({
|
|
403
|
+
id: row.id,
|
|
404
|
+
entryKey: row.entry_key,
|
|
405
|
+
dirPath: row.dir_path,
|
|
406
|
+
filePath: row.file_path,
|
|
407
|
+
stashDir: row.stash_dir,
|
|
408
|
+
entry: JSON.parse(row.entry_json) as StashEntry,
|
|
409
|
+
searchText: row.search_text,
|
|
410
|
+
}))
|
|
411
|
+
}
|
package/src/embedder.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { EmbeddingConnectionConfig } from "./config"
|
|
2
|
+
import { fetchWithTimeout } from "./common"
|
|
2
3
|
|
|
3
4
|
// ── Types ───────────────────────────────────────────────────────────────────
|
|
4
5
|
|
|
@@ -6,22 +7,27 @@ export type EmbeddingVector = number[]
|
|
|
6
7
|
|
|
7
8
|
// ── Singleton local embedder ────────────────────────────────────────────────
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
interface TransformerPipeline {
|
|
11
|
+
(text: string, options: { pooling: string; normalize: boolean }): Promise<{ data: Float32Array }>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let localEmbedder: TransformerPipeline | undefined
|
|
10
15
|
|
|
11
|
-
async function getLocalEmbedder(): Promise<
|
|
16
|
+
async function getLocalEmbedder(): Promise<TransformerPipeline> {
|
|
12
17
|
if (!localEmbedder) {
|
|
13
|
-
let pipeline:
|
|
18
|
+
let pipeline: unknown
|
|
14
19
|
try {
|
|
15
20
|
const mod = await import("@xenova/transformers")
|
|
16
|
-
pipeline = mod.pipeline
|
|
21
|
+
pipeline = mod.pipeline as unknown
|
|
17
22
|
} catch {
|
|
18
23
|
throw new Error(
|
|
19
24
|
"Semantic search requires @xenova/transformers. Install it with: npm install @xenova/transformers",
|
|
20
25
|
)
|
|
21
26
|
}
|
|
22
|
-
|
|
27
|
+
const pipelineFn = pipeline as (task: string, model: string) => Promise<TransformerPipeline>
|
|
28
|
+
localEmbedder = await pipelineFn("feature-extraction", "Xenova/all-MiniLM-L6-v2")
|
|
23
29
|
}
|
|
24
|
-
return localEmbedder
|
|
30
|
+
return localEmbedder!
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
async function embedLocal(text: string): Promise<EmbeddingVector> {
|
|
@@ -41,13 +47,18 @@ async function embedRemote(
|
|
|
41
47
|
headers["Authorization"] = `Bearer ${config.apiKey}`
|
|
42
48
|
}
|
|
43
49
|
|
|
44
|
-
const
|
|
50
|
+
const body: { input: string; model: string; dimensions?: number } = {
|
|
51
|
+
input: text,
|
|
52
|
+
model: config.model,
|
|
53
|
+
}
|
|
54
|
+
if (config.dimension) {
|
|
55
|
+
body.dimensions = config.dimension
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const response = await fetchWithTimeout(config.endpoint, {
|
|
45
59
|
method: "POST",
|
|
46
60
|
headers,
|
|
47
|
-
body: JSON.stringify(
|
|
48
|
-
input: text,
|
|
49
|
-
model: config.model,
|
|
50
|
-
}),
|
|
61
|
+
body: JSON.stringify(body),
|
|
51
62
|
})
|
|
52
63
|
|
|
53
64
|
if (!response.ok) {
|
package/src/github.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const GITHUB_API_BASE = "https://api.github.com"
|
|
2
|
+
|
|
3
|
+
export function githubHeaders(): HeadersInit {
|
|
4
|
+
const token = process.env.GITHUB_TOKEN?.trim()
|
|
5
|
+
const headers: Record<string, string> = {
|
|
6
|
+
Accept: "application/vnd.github+json",
|
|
7
|
+
"User-Agent": "agentikit-registry",
|
|
8
|
+
}
|
|
9
|
+
if (token) headers.Authorization = `Bearer ${token}`
|
|
10
|
+
return headers
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function asRecord(value: unknown): Record<string, unknown> {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
15
|
+
? value as Record<string, unknown>
|
|
16
|
+
: {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function asString(value: unknown): string | undefined {
|
|
20
|
+
return typeof value === "string" && value ? value : undefined
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { parseFrontmatter, toStringOrUndefined } from "../frontmatter"
|
|
2
|
+
import { isMarkdownFile, markdownCanonicalName, markdownAssetPath } from "./markdown-helpers"
|
|
3
|
+
import type { AssetTypeHandler, ShowInput } from "../asset-type-handler"
|
|
4
|
+
import type { ShowResponse } from "../stash-types"
|
|
5
|
+
|
|
6
|
+
export const agentHandler: AssetTypeHandler = {
|
|
7
|
+
typeName: "agent",
|
|
8
|
+
stashDir: "agents",
|
|
9
|
+
|
|
10
|
+
isRelevantFile: isMarkdownFile,
|
|
11
|
+
toCanonicalName: markdownCanonicalName,
|
|
12
|
+
toAssetPath: markdownAssetPath,
|
|
13
|
+
|
|
14
|
+
buildShowResponse(input: ShowInput): ShowResponse {
|
|
15
|
+
const parsedMd = parseFrontmatter(input.content)
|
|
16
|
+
return {
|
|
17
|
+
type: "agent",
|
|
18
|
+
name: input.name,
|
|
19
|
+
path: input.path,
|
|
20
|
+
description: toStringOrUndefined(parsedMd.data.description),
|
|
21
|
+
prompt: "Dispatching prompt must include the agent's full prompt content verbatim; summaries are non-compliant. \n\n"
|
|
22
|
+
+ parsedMd.content,
|
|
23
|
+
toolPolicy: parsedMd.data.tools,
|
|
24
|
+
modelHint: parsedMd.data.model,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
defaultUsageGuide: [
|
|
29
|
+
"Read the .md file and dispatch an agent using the content of the file. Use modelHint/toolPolicy when present to run the agent with compatible settings.",
|
|
30
|
+
"Use with `akm show <openRef>` to get the full prompt payload.",
|
|
31
|
+
],
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { parseFrontmatter, toStringOrUndefined } from "../frontmatter"
|
|
2
|
+
import { isMarkdownFile, markdownCanonicalName, markdownAssetPath } from "./markdown-helpers"
|
|
3
|
+
import type { AssetTypeHandler, ShowInput } from "../asset-type-handler"
|
|
4
|
+
import type { ShowResponse } from "../stash-types"
|
|
5
|
+
|
|
6
|
+
export const commandHandler: AssetTypeHandler = {
|
|
7
|
+
typeName: "command",
|
|
8
|
+
stashDir: "commands",
|
|
9
|
+
|
|
10
|
+
isRelevantFile: isMarkdownFile,
|
|
11
|
+
toCanonicalName: markdownCanonicalName,
|
|
12
|
+
toAssetPath: markdownAssetPath,
|
|
13
|
+
|
|
14
|
+
buildShowResponse(input: ShowInput): ShowResponse {
|
|
15
|
+
const parsedMd = parseFrontmatter(input.content)
|
|
16
|
+
return {
|
|
17
|
+
type: "command",
|
|
18
|
+
name: input.name,
|
|
19
|
+
path: input.path,
|
|
20
|
+
description: toStringOrUndefined(parsedMd.data.description),
|
|
21
|
+
template: parsedMd.content,
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
defaultUsageGuide: [
|
|
26
|
+
"Read the .md file, fill placeholders, and run it in the current repo context.",
|
|
27
|
+
"Use `akm show <openRef>` to retrieve the command template body.",
|
|
28
|
+
],
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { registerAssetType } from "../asset-type-handler"
|
|
2
|
+
import { toolHandler } from "./tool-handler"
|
|
3
|
+
import { skillHandler } from "./skill-handler"
|
|
4
|
+
import { commandHandler } from "./command-handler"
|
|
5
|
+
import { agentHandler } from "./agent-handler"
|
|
6
|
+
import { knowledgeHandler } from "./knowledge-handler"
|
|
7
|
+
import { scriptHandler } from "./script-handler"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Register all built-in asset type handlers.
|
|
11
|
+
* This must be called (imported) before any handler lookups.
|
|
12
|
+
*/
|
|
13
|
+
registerAssetType(toolHandler)
|
|
14
|
+
registerAssetType(skillHandler)
|
|
15
|
+
registerAssetType(commandHandler)
|
|
16
|
+
registerAssetType(agentHandler)
|
|
17
|
+
registerAssetType(knowledgeHandler)
|
|
18
|
+
registerAssetType(scriptHandler)
|
|
19
|
+
|
|
20
|
+
export { toolHandler } from "./tool-handler"
|
|
21
|
+
export { skillHandler } from "./skill-handler"
|
|
22
|
+
export { commandHandler } from "./command-handler"
|
|
23
|
+
export { agentHandler } from "./agent-handler"
|
|
24
|
+
export { knowledgeHandler } from "./knowledge-handler"
|
|
25
|
+
export { scriptHandler } from "./script-handler"
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import { parseMarkdownToc, extractSection, extractLineRange, extractFrontmatterOnly, formatToc } from "../markdown"
|
|
3
|
+
import { isMarkdownFile, markdownCanonicalName, markdownAssetPath } from "./markdown-helpers"
|
|
4
|
+
import type { AssetTypeHandler, ShowInput } from "../asset-type-handler"
|
|
5
|
+
import type { ShowResponse } from "../stash-types"
|
|
6
|
+
import type { StashEntry } from "../metadata"
|
|
7
|
+
|
|
8
|
+
export const knowledgeHandler: AssetTypeHandler = {
|
|
9
|
+
typeName: "knowledge",
|
|
10
|
+
stashDir: "knowledge",
|
|
11
|
+
|
|
12
|
+
isRelevantFile: isMarkdownFile,
|
|
13
|
+
toCanonicalName: markdownCanonicalName,
|
|
14
|
+
toAssetPath: markdownAssetPath,
|
|
15
|
+
|
|
16
|
+
buildShowResponse(input: ShowInput): ShowResponse {
|
|
17
|
+
const v = input.view ?? { mode: "full" }
|
|
18
|
+
switch (v.mode) {
|
|
19
|
+
case "toc": {
|
|
20
|
+
const toc = parseMarkdownToc(input.content)
|
|
21
|
+
return { type: "knowledge", name: input.name, path: input.path, content: formatToc(toc) }
|
|
22
|
+
}
|
|
23
|
+
case "frontmatter": {
|
|
24
|
+
const fm = extractFrontmatterOnly(input.content)
|
|
25
|
+
return { type: "knowledge", name: input.name, path: input.path, content: fm ?? "(no frontmatter)" }
|
|
26
|
+
}
|
|
27
|
+
case "section": {
|
|
28
|
+
const section = extractSection(input.content, v.heading)
|
|
29
|
+
if (!section) {
|
|
30
|
+
return {
|
|
31
|
+
type: "knowledge",
|
|
32
|
+
name: input.name,
|
|
33
|
+
path: input.path,
|
|
34
|
+
content: `Section "${v.heading}" not found in ${input.name}. Try --view toc to discover available headings.`,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { type: "knowledge", name: input.name, path: input.path, content: section.content }
|
|
38
|
+
}
|
|
39
|
+
case "lines": {
|
|
40
|
+
return { type: "knowledge", name: input.name, path: input.path, content: extractLineRange(input.content, v.start, v.end) }
|
|
41
|
+
}
|
|
42
|
+
default: {
|
|
43
|
+
return { type: "knowledge", name: input.name, path: input.path, content: input.content }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
defaultUsageGuide: [
|
|
49
|
+
"Use `akm show <openRef>` to read the document; start with `--view toc` for large files.",
|
|
50
|
+
"Use `--view section` or `--view lines` to load only the part you need.",
|
|
51
|
+
],
|
|
52
|
+
|
|
53
|
+
extractTypeMetadata(entry: StashEntry, file: string): void {
|
|
54
|
+
try {
|
|
55
|
+
const mdContent = fs.readFileSync(file, "utf8")
|
|
56
|
+
const toc = parseMarkdownToc(mdContent)
|
|
57
|
+
if (toc.headings.length > 0) entry.toc = toc.headings
|
|
58
|
+
} catch {
|
|
59
|
+
// Non-fatal: skip TOC if file can't be read
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import { toPosix } from "../common"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shared file-system helpers for markdown-based asset types
|
|
6
|
+
* (command, agent, knowledge).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function isMarkdownFile(fileName: string): boolean {
|
|
10
|
+
return path.extname(fileName).toLowerCase() === ".md"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function markdownCanonicalName(typeRoot: string, filePath: string): string | undefined {
|
|
14
|
+
return toPosix(path.relative(typeRoot, filePath))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function markdownAssetPath(typeRoot: string, name: string): string {
|
|
18
|
+
return path.join(typeRoot, name)
|
|
19
|
+
}
|