agentikit 0.0.9 → 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 +129 -214
- package/dist/index.d.ts +8 -2
- package/dist/index.js +4 -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 +201 -75
- 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 +19 -6
- package/dist/src/config.js +139 -29
- 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 +0 -1
- package/dist/src/metadata.js +6 -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 +2 -2
- package/dist/src/registry-install.js +142 -35
- package/dist/src/registry-resolve.js +90 -22
- package/dist/src/registry-search.d.ts +22 -0
- package/dist/src/registry-search.js +231 -97
- package/dist/src/registry-types.d.ts +9 -2
- package/dist/src/stash-add.js +4 -4
- 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.js +12 -12
- package/dist/src/stash-resolve.js +3 -0
- package/dist/src/stash-search.js +168 -164
- package/dist/src/stash-show.d.ts +1 -1
- package/dist/src/stash-show.js +28 -96
- package/dist/src/stash-source.d.ts +24 -0
- package/dist/src/stash-source.js +81 -0
- package/dist/src/stash-types.d.ts +14 -4
- package/dist/src/stash.d.ts +6 -0
- package/dist/src/stash.js +3 -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 +213 -82
- package/src/common.ts +23 -5
- package/src/config-cli.ts +499 -0
- package/src/config.ts +160 -38
- 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 +5 -65
- package/src/origin-resolve.ts +67 -0
- package/src/registry-install.ts +158 -42
- package/src/registry-resolve.ts +92 -23
- package/src/registry-search.ts +288 -98
- package/src/registry-types.ts +10 -2
- package/src/stash-add.ts +14 -17
- package/src/stash-clone.ts +127 -0
- package/src/stash-ref.ts +84 -26
- package/src/stash-registry.ts +12 -12
- package/src/stash-resolve.ts +3 -0
- package/src/stash-search.ts +202 -184
- package/src/stash-show.ts +33 -90
- package/src/stash-source.ts +103 -0
- package/src/stash-types.ts +14 -4
- package/src/stash.ts +8 -0
- package/src/tool-runner.ts +18 -5
- package/dist/src/similarity.d.ts +0 -34
- package/src/similarity.ts +0 -271
package/dist/src/config.js
CHANGED
|
@@ -1,41 +1,93 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { resolveStashDir } from "./common";
|
|
4
3
|
// ── Defaults ────────────────────────────────────────────────────────────────
|
|
5
4
|
export const DEFAULT_CONFIG = {
|
|
6
5
|
semanticSearch: true,
|
|
7
|
-
|
|
6
|
+
mountedStashDirs: [],
|
|
8
7
|
};
|
|
9
8
|
// ── Paths ───────────────────────────────────────────────────────────────────
|
|
10
|
-
export function
|
|
11
|
-
|
|
9
|
+
export function getConfigDir(env = process.env, platform = process.platform) {
|
|
10
|
+
if (platform === "win32") {
|
|
11
|
+
const appData = env.APPDATA?.trim();
|
|
12
|
+
if (appData)
|
|
13
|
+
return path.join(appData, "agentikit");
|
|
14
|
+
const userProfile = env.USERPROFILE?.trim();
|
|
15
|
+
if (!userProfile) {
|
|
16
|
+
throw new Error("Unable to determine config directory. Set APPDATA or USERPROFILE.");
|
|
17
|
+
}
|
|
18
|
+
return path.join(userProfile, "AppData", "Roaming", "agentikit");
|
|
19
|
+
}
|
|
20
|
+
const xdgConfigHome = env.XDG_CONFIG_HOME?.trim();
|
|
21
|
+
if (xdgConfigHome)
|
|
22
|
+
return path.join(xdgConfigHome, "agentikit");
|
|
23
|
+
const home = env.HOME?.trim();
|
|
24
|
+
if (!home) {
|
|
25
|
+
throw new Error("Unable to determine config directory. Set XDG_CONFIG_HOME or HOME.");
|
|
26
|
+
}
|
|
27
|
+
return path.join(home, ".config", "agentikit");
|
|
28
|
+
}
|
|
29
|
+
export function getConfigPath() {
|
|
30
|
+
return path.join(getConfigDir(), "config.json");
|
|
12
31
|
}
|
|
13
32
|
// ── Load / Save / Update ────────────────────────────────────────────────────
|
|
14
|
-
export function loadConfig(
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
33
|
+
export function loadConfig() {
|
|
34
|
+
const configPath = getConfigPath();
|
|
35
|
+
const raw = readConfigObject(configPath);
|
|
36
|
+
const config = raw ? pickKnownKeys(raw) : { ...DEFAULT_CONFIG };
|
|
37
|
+
// Inject API keys from environment variables.
|
|
38
|
+
// API keys should be provided via AKM_EMBED_API_KEY and AKM_LLM_API_KEY
|
|
39
|
+
// rather than stored in the config file.
|
|
40
|
+
if (config.embedding && !config.embedding.apiKey) {
|
|
41
|
+
const envKey = process.env.AKM_EMBED_API_KEY?.trim();
|
|
42
|
+
if (envKey)
|
|
43
|
+
config.embedding.apiKey = envKey;
|
|
44
|
+
}
|
|
45
|
+
if (config.llm && !config.llm.apiKey) {
|
|
46
|
+
const envKey = process.env.AKM_LLM_API_KEY?.trim();
|
|
47
|
+
if (envKey)
|
|
48
|
+
config.llm.apiKey = envKey;
|
|
49
|
+
}
|
|
50
|
+
return config;
|
|
51
|
+
}
|
|
52
|
+
export function saveConfig(config) {
|
|
53
|
+
const configPath = getConfigPath();
|
|
54
|
+
const dir = path.dirname(configPath);
|
|
55
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
56
|
+
const sanitized = sanitizeConfigForWrite(config);
|
|
57
|
+
const tmpPath = configPath + `.tmp.${process.pid}`;
|
|
18
58
|
try {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return { ...DEFAULT_CONFIG };
|
|
22
|
-
}
|
|
59
|
+
fs.writeFileSync(tmpPath, JSON.stringify(sanitized, null, 2) + "\n", "utf8");
|
|
60
|
+
fs.renameSync(tmpPath, configPath);
|
|
23
61
|
}
|
|
24
|
-
catch {
|
|
25
|
-
|
|
62
|
+
catch (err) {
|
|
63
|
+
try {
|
|
64
|
+
fs.unlinkSync(tmpPath);
|
|
65
|
+
}
|
|
66
|
+
catch { /* ignore cleanup failure */ }
|
|
67
|
+
throw err;
|
|
26
68
|
}
|
|
27
|
-
return pickKnownKeys(raw);
|
|
28
69
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Strip apiKey fields before writing config to disk.
|
|
72
|
+
* API keys should be provided via environment variables
|
|
73
|
+
* AKM_EMBED_API_KEY and AKM_LLM_API_KEY.
|
|
74
|
+
*/
|
|
75
|
+
function sanitizeConfigForWrite(config) {
|
|
76
|
+
const sanitized = { ...config };
|
|
77
|
+
if (sanitized.embedding) {
|
|
78
|
+
const { apiKey, ...rest } = sanitized.embedding;
|
|
79
|
+
sanitized.embedding = rest;
|
|
80
|
+
}
|
|
81
|
+
if (sanitized.llm) {
|
|
82
|
+
const { apiKey, ...rest } = sanitized.llm;
|
|
83
|
+
sanitized.llm = rest;
|
|
84
|
+
}
|
|
85
|
+
return sanitized;
|
|
33
86
|
}
|
|
34
|
-
export function updateConfig(partial
|
|
35
|
-
const
|
|
36
|
-
const current = loadConfig(dir);
|
|
87
|
+
export function updateConfig(partial) {
|
|
88
|
+
const current = loadConfig();
|
|
37
89
|
const merged = { ...current, ...partial };
|
|
38
|
-
saveConfig(merged
|
|
90
|
+
saveConfig(merged);
|
|
39
91
|
return merged;
|
|
40
92
|
}
|
|
41
93
|
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
@@ -44,21 +96,35 @@ function pickKnownKeys(raw) {
|
|
|
44
96
|
if (typeof raw.semanticSearch === "boolean") {
|
|
45
97
|
config.semanticSearch = raw.semanticSearch;
|
|
46
98
|
}
|
|
47
|
-
if (Array.isArray(raw.
|
|
48
|
-
config.
|
|
99
|
+
if (Array.isArray(raw.mountedStashDirs)) {
|
|
100
|
+
config.mountedStashDirs = raw.mountedStashDirs.filter((d) => typeof d === "string");
|
|
49
101
|
}
|
|
50
|
-
const embedding =
|
|
102
|
+
const embedding = parseEmbeddingConfig(raw.embedding);
|
|
51
103
|
if (embedding)
|
|
52
104
|
config.embedding = embedding;
|
|
53
|
-
const llm =
|
|
105
|
+
const llm = parseLlmConfig(raw.llm);
|
|
54
106
|
if (llm)
|
|
55
107
|
config.llm = llm;
|
|
56
108
|
const registry = parseRegistryConfig(raw.registry);
|
|
57
109
|
if (registry)
|
|
58
110
|
config.registry = registry;
|
|
111
|
+
if (Array.isArray(raw.registryUrls)) {
|
|
112
|
+
config.registryUrls = raw.registryUrls.filter((u) => typeof u === "string" && u.startsWith("http"));
|
|
113
|
+
}
|
|
59
114
|
return config;
|
|
60
115
|
}
|
|
61
|
-
function
|
|
116
|
+
function readConfigObject(configPath) {
|
|
117
|
+
try {
|
|
118
|
+
const raw = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
|
119
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
120
|
+
return undefined;
|
|
121
|
+
return raw;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function parseEmbeddingConfig(value) {
|
|
62
128
|
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
63
129
|
return undefined;
|
|
64
130
|
const obj = value;
|
|
@@ -70,6 +136,50 @@ function parseConnectionConfig(value) {
|
|
|
70
136
|
endpoint: obj.endpoint,
|
|
71
137
|
model: obj.model,
|
|
72
138
|
};
|
|
139
|
+
if (typeof obj.provider === "string" && obj.provider) {
|
|
140
|
+
result.provider = obj.provider;
|
|
141
|
+
}
|
|
142
|
+
if ("dimension" in obj) {
|
|
143
|
+
if (typeof obj.dimension !== "number" ||
|
|
144
|
+
!Number.isFinite(obj.dimension) ||
|
|
145
|
+
!Number.isInteger(obj.dimension) ||
|
|
146
|
+
obj.dimension <= 0) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
result.dimension = obj.dimension;
|
|
150
|
+
}
|
|
151
|
+
if (typeof obj.apiKey === "string" && obj.apiKey) {
|
|
152
|
+
result.apiKey = obj.apiKey;
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
function parseLlmConfig(value) {
|
|
157
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
158
|
+
return undefined;
|
|
159
|
+
const obj = value;
|
|
160
|
+
if (typeof obj.endpoint !== "string" || !obj.endpoint)
|
|
161
|
+
return undefined;
|
|
162
|
+
if (typeof obj.model !== "string" || !obj.model)
|
|
163
|
+
return undefined;
|
|
164
|
+
const result = {
|
|
165
|
+
endpoint: obj.endpoint,
|
|
166
|
+
model: obj.model,
|
|
167
|
+
};
|
|
168
|
+
if (typeof obj.provider === "string" && obj.provider) {
|
|
169
|
+
result.provider = obj.provider;
|
|
170
|
+
}
|
|
171
|
+
if (typeof obj.temperature === "number" && Number.isFinite(obj.temperature)) {
|
|
172
|
+
result.temperature = obj.temperature;
|
|
173
|
+
}
|
|
174
|
+
if ("maxTokens" in obj) {
|
|
175
|
+
if (typeof obj.maxTokens !== "number" ||
|
|
176
|
+
!Number.isFinite(obj.maxTokens) ||
|
|
177
|
+
!Number.isInteger(obj.maxTokens) ||
|
|
178
|
+
obj.maxTokens <= 0) {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
result.maxTokens = obj.maxTokens;
|
|
182
|
+
}
|
|
73
183
|
if (typeof obj.apiKey === "string" && obj.apiKey) {
|
|
74
184
|
result.apiKey = obj.apiKey;
|
|
75
185
|
}
|
|
@@ -120,5 +230,5 @@ function asNonEmptyString(value) {
|
|
|
120
230
|
return typeof value === "string" && value ? value : undefined;
|
|
121
231
|
}
|
|
122
232
|
function asRegistrySource(value) {
|
|
123
|
-
return value === "npm" || value === "github" ? value : undefined;
|
|
233
|
+
return value === "npm" || value === "github" || value === "git" ? value : undefined;
|
|
124
234
|
}
|
package/dist/src/db.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import type { StashEntry } from "./metadata";
|
|
3
|
+
import type { EmbeddingVector } from "./embedder";
|
|
4
|
+
export interface DbIndexedEntry {
|
|
5
|
+
id: number;
|
|
6
|
+
entryKey: string;
|
|
7
|
+
dirPath: string;
|
|
8
|
+
filePath: string;
|
|
9
|
+
stashDir: string;
|
|
10
|
+
entry: StashEntry;
|
|
11
|
+
searchText: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DbSearchResult {
|
|
14
|
+
id: number;
|
|
15
|
+
filePath: string;
|
|
16
|
+
entry: StashEntry;
|
|
17
|
+
searchText: string;
|
|
18
|
+
bm25Score: number;
|
|
19
|
+
}
|
|
20
|
+
export interface DbVecResult {
|
|
21
|
+
id: number;
|
|
22
|
+
distance: number;
|
|
23
|
+
}
|
|
24
|
+
export declare const DB_VERSION = 5;
|
|
25
|
+
export declare const EMBEDDING_DIM = 384;
|
|
26
|
+
export declare function getDbPath(): string;
|
|
27
|
+
export declare function openDatabase(dbPath?: string, options?: {
|
|
28
|
+
embeddingDim?: number;
|
|
29
|
+
}): Database;
|
|
30
|
+
export declare function closeDatabase(db: Database): void;
|
|
31
|
+
export declare function isVecAvailable(): boolean;
|
|
32
|
+
export declare function getMeta(db: Database, key: string): string | undefined;
|
|
33
|
+
export declare function setMeta(db: Database, key: string, value: string): void;
|
|
34
|
+
export declare function upsertEntry(db: Database, entryKey: string, dirPath: string, filePath: string, stashDir: string, entry: StashEntry, searchText: string): number;
|
|
35
|
+
export declare function deleteEntriesByDir(db: Database, dirPath: string): void;
|
|
36
|
+
export declare function rebuildFts(db: Database): void;
|
|
37
|
+
export declare function upsertEmbedding(db: Database, entryId: number, embedding: EmbeddingVector): void;
|
|
38
|
+
export declare function searchVec(db: Database, queryEmbedding: EmbeddingVector, k: number): DbVecResult[];
|
|
39
|
+
export declare function searchFts(db: Database, query: string, limit: number, entryType?: string): DbSearchResult[];
|
|
40
|
+
export declare function getAllEntries(db: Database, entryType?: string): DbIndexedEntry[];
|
|
41
|
+
export declare function getEntryCount(db: Database): number;
|
|
42
|
+
export declare function getEntryById(db: Database, id: number): {
|
|
43
|
+
filePath: string;
|
|
44
|
+
entry: StashEntry;
|
|
45
|
+
} | undefined;
|
|
46
|
+
export declare function getEntriesByDir(db: Database, dirPath: string): DbIndexedEntry[];
|
package/dist/src/db.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { Database } from "bun:sqlite";
|
|
4
|
+
// ── Constants ───────────────────────────────────────────────────────────────
|
|
5
|
+
export const DB_VERSION = 5;
|
|
6
|
+
export const EMBEDDING_DIM = 384;
|
|
7
|
+
// ── Path ────────────────────────────────────────────────────────────────────
|
|
8
|
+
export function getDbPath() {
|
|
9
|
+
const cacheDir = process.env.XDG_CACHE_HOME ||
|
|
10
|
+
path.join(process.env.HOME || process.env.USERPROFILE || "", ".cache");
|
|
11
|
+
return path.join(cacheDir, "agentikit", "index.db");
|
|
12
|
+
}
|
|
13
|
+
// ── Database lifecycle ──────────────────────────────────────────────────────
|
|
14
|
+
export function openDatabase(dbPath, options) {
|
|
15
|
+
const resolvedPath = dbPath ?? getDbPath();
|
|
16
|
+
const dir = path.dirname(resolvedPath);
|
|
17
|
+
if (!fs.existsSync(dir)) {
|
|
18
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
const db = new Database(resolvedPath);
|
|
21
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
22
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
23
|
+
// Try to load sqlite-vec extension
|
|
24
|
+
loadVecExtension(db);
|
|
25
|
+
ensureSchema(db, options?.embeddingDim ?? EMBEDDING_DIM);
|
|
26
|
+
return db;
|
|
27
|
+
}
|
|
28
|
+
export function closeDatabase(db) {
|
|
29
|
+
db.close();
|
|
30
|
+
}
|
|
31
|
+
// ── sqlite-vec extension ────────────────────────────────────────────────────
|
|
32
|
+
let vecAvailable = false;
|
|
33
|
+
function loadVecExtension(db) {
|
|
34
|
+
try {
|
|
35
|
+
const sqliteVec = require("sqlite-vec");
|
|
36
|
+
sqliteVec.load(db);
|
|
37
|
+
vecAvailable = true;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
console.warn("sqlite-vec extension not available, embeddings will be skipped");
|
|
41
|
+
vecAvailable = false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function isVecAvailable() {
|
|
45
|
+
return vecAvailable;
|
|
46
|
+
}
|
|
47
|
+
// ── Schema ──────────────────────────────────────────────────────────────────
|
|
48
|
+
function ensureSchema(db, embeddingDim) {
|
|
49
|
+
// Create meta table first so we can check version
|
|
50
|
+
db.exec(`
|
|
51
|
+
CREATE TABLE IF NOT EXISTS index_meta (
|
|
52
|
+
key TEXT PRIMARY KEY,
|
|
53
|
+
value TEXT NOT NULL
|
|
54
|
+
);
|
|
55
|
+
`);
|
|
56
|
+
// Check stored version — if it differs from DB_VERSION, drop and recreate all tables
|
|
57
|
+
const storedVersion = getMeta(db, "version");
|
|
58
|
+
if (storedVersion && storedVersion !== String(DB_VERSION)) {
|
|
59
|
+
db.exec("DROP TABLE IF EXISTS entries_vec");
|
|
60
|
+
db.exec("DROP TABLE IF EXISTS entries_fts");
|
|
61
|
+
db.exec("DROP INDEX IF EXISTS idx_entries_dir");
|
|
62
|
+
db.exec("DROP INDEX IF EXISTS idx_entries_type");
|
|
63
|
+
db.exec("DROP TABLE IF EXISTS entries");
|
|
64
|
+
db.exec("DELETE FROM index_meta");
|
|
65
|
+
}
|
|
66
|
+
db.exec(`
|
|
67
|
+
CREATE TABLE IF NOT EXISTS entries (
|
|
68
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
69
|
+
entry_key TEXT NOT NULL UNIQUE,
|
|
70
|
+
dir_path TEXT NOT NULL,
|
|
71
|
+
file_path TEXT NOT NULL,
|
|
72
|
+
stash_dir TEXT NOT NULL,
|
|
73
|
+
entry_json TEXT NOT NULL,
|
|
74
|
+
search_text TEXT NOT NULL,
|
|
75
|
+
entry_type TEXT NOT NULL
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
CREATE INDEX IF NOT EXISTS idx_entries_dir ON entries(dir_path);
|
|
79
|
+
CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type);
|
|
80
|
+
`);
|
|
81
|
+
// FTS5 table — standalone with explicit entry_id for joining
|
|
82
|
+
const ftsExists = db
|
|
83
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='entries_fts'")
|
|
84
|
+
.get();
|
|
85
|
+
if (!ftsExists) {
|
|
86
|
+
db.exec(`
|
|
87
|
+
CREATE VIRTUAL TABLE entries_fts USING fts5(
|
|
88
|
+
entry_id UNINDEXED,
|
|
89
|
+
search_text,
|
|
90
|
+
tokenize='porter unicode61'
|
|
91
|
+
);
|
|
92
|
+
`);
|
|
93
|
+
}
|
|
94
|
+
// sqlite-vec table
|
|
95
|
+
if (vecAvailable) {
|
|
96
|
+
// Check if stored embedding dimension differs from configured one
|
|
97
|
+
const storedDim = getMeta(db, "embeddingDim");
|
|
98
|
+
if (storedDim && storedDim !== String(embeddingDim)) {
|
|
99
|
+
try {
|
|
100
|
+
db.exec("DROP TABLE IF EXISTS entries_vec");
|
|
101
|
+
}
|
|
102
|
+
catch { /* ignore */ }
|
|
103
|
+
}
|
|
104
|
+
const vecExists = db
|
|
105
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='entries_vec'")
|
|
106
|
+
.get();
|
|
107
|
+
if (!vecExists) {
|
|
108
|
+
db.exec(`
|
|
109
|
+
CREATE VIRTUAL TABLE entries_vec USING vec0(
|
|
110
|
+
id INTEGER PRIMARY KEY,
|
|
111
|
+
embedding FLOAT[${embeddingDim}]
|
|
112
|
+
);
|
|
113
|
+
`);
|
|
114
|
+
}
|
|
115
|
+
setMeta(db, "embeddingDim", String(embeddingDim));
|
|
116
|
+
}
|
|
117
|
+
// Set version if not present
|
|
118
|
+
const version = getMeta(db, "version");
|
|
119
|
+
if (!version) {
|
|
120
|
+
setMeta(db, "version", String(DB_VERSION));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// ── Meta helpers ────────────────────────────────────────────────────────────
|
|
124
|
+
export function getMeta(db, key) {
|
|
125
|
+
const row = db.prepare("SELECT value FROM index_meta WHERE key = ?").get(key);
|
|
126
|
+
return row?.value;
|
|
127
|
+
}
|
|
128
|
+
export function setMeta(db, key, value) {
|
|
129
|
+
db.prepare("INSERT OR REPLACE INTO index_meta (key, value) VALUES (?, ?)").run(key, value);
|
|
130
|
+
}
|
|
131
|
+
// ── Entry operations ────────────────────────────────────────────────────────
|
|
132
|
+
export function upsertEntry(db, entryKey, dirPath, filePath, stashDir, entry, searchText) {
|
|
133
|
+
const stmt = db.prepare(`
|
|
134
|
+
INSERT INTO entries (entry_key, dir_path, file_path, stash_dir, entry_json, search_text, entry_type)
|
|
135
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
136
|
+
ON CONFLICT(entry_key) DO UPDATE SET
|
|
137
|
+
dir_path = excluded.dir_path,
|
|
138
|
+
file_path = excluded.file_path,
|
|
139
|
+
stash_dir = excluded.stash_dir,
|
|
140
|
+
entry_json = excluded.entry_json,
|
|
141
|
+
search_text = excluded.search_text,
|
|
142
|
+
entry_type = excluded.entry_type
|
|
143
|
+
`);
|
|
144
|
+
stmt.run(entryKey, dirPath, filePath, stashDir, JSON.stringify(entry), searchText, entry.type);
|
|
145
|
+
// Fetch the row id explicitly since last_insert_rowid() is unreliable for ON CONFLICT DO UPDATE
|
|
146
|
+
const row = db.prepare("SELECT id FROM entries WHERE entry_key = ?").get(entryKey);
|
|
147
|
+
return row.id;
|
|
148
|
+
}
|
|
149
|
+
export function deleteEntriesByDir(db, dirPath) {
|
|
150
|
+
if (vecAvailable) {
|
|
151
|
+
const ids = db
|
|
152
|
+
.prepare("SELECT id FROM entries WHERE dir_path = ?")
|
|
153
|
+
.all(dirPath);
|
|
154
|
+
for (const { id } of ids) {
|
|
155
|
+
try {
|
|
156
|
+
db.prepare("DELETE FROM entries_vec WHERE id = ?").run(id);
|
|
157
|
+
}
|
|
158
|
+
catch { /* ignore if vec table missing */ }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
db.prepare("DELETE FROM entries WHERE dir_path = ?").run(dirPath);
|
|
162
|
+
}
|
|
163
|
+
export function rebuildFts(db) {
|
|
164
|
+
db.exec("DELETE FROM entries_fts");
|
|
165
|
+
db.exec("INSERT INTO entries_fts (entry_id, search_text) SELECT CAST(id AS TEXT), search_text FROM entries");
|
|
166
|
+
}
|
|
167
|
+
// ── Vector operations ───────────────────────────────────────────────────────
|
|
168
|
+
export function upsertEmbedding(db, entryId, embedding) {
|
|
169
|
+
if (!vecAvailable)
|
|
170
|
+
return;
|
|
171
|
+
const buf = float32Buffer(embedding);
|
|
172
|
+
try {
|
|
173
|
+
db.prepare("DELETE FROM entries_vec WHERE id = ?").run(entryId);
|
|
174
|
+
}
|
|
175
|
+
catch { /* ignore */ }
|
|
176
|
+
db.prepare("INSERT INTO entries_vec (id, embedding) VALUES (?, ?)").run(entryId, buf);
|
|
177
|
+
}
|
|
178
|
+
export function searchVec(db, queryEmbedding, k) {
|
|
179
|
+
if (!vecAvailable)
|
|
180
|
+
return [];
|
|
181
|
+
const buf = float32Buffer(queryEmbedding);
|
|
182
|
+
try {
|
|
183
|
+
return db
|
|
184
|
+
.prepare("SELECT id, distance FROM entries_vec WHERE embedding MATCH ? AND k = ?")
|
|
185
|
+
.all(buf, k);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function float32Buffer(vec) {
|
|
192
|
+
const f32 = new Float32Array(vec);
|
|
193
|
+
return Buffer.from(f32.buffer);
|
|
194
|
+
}
|
|
195
|
+
// ── FTS5 search ─────────────────────────────────────────────────────────────
|
|
196
|
+
export function searchFts(db, query, limit, entryType) {
|
|
197
|
+
const ftsQuery = sanitizeFtsQuery(query);
|
|
198
|
+
if (!ftsQuery)
|
|
199
|
+
return [];
|
|
200
|
+
let sql;
|
|
201
|
+
let params;
|
|
202
|
+
if (entryType && entryType !== "any") {
|
|
203
|
+
sql = `
|
|
204
|
+
SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
|
|
205
|
+
bm25(entries_fts) AS bm25Score
|
|
206
|
+
FROM entries_fts f
|
|
207
|
+
JOIN entries e ON e.id = CAST(f.entry_id AS INTEGER)
|
|
208
|
+
WHERE entries_fts MATCH ?
|
|
209
|
+
AND e.entry_type = ?
|
|
210
|
+
ORDER BY bm25Score
|
|
211
|
+
LIMIT ?
|
|
212
|
+
`;
|
|
213
|
+
params = [ftsQuery, entryType, limit];
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
sql = `
|
|
217
|
+
SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
|
|
218
|
+
bm25(entries_fts) AS bm25Score
|
|
219
|
+
FROM entries_fts f
|
|
220
|
+
JOIN entries e ON e.id = CAST(f.entry_id AS INTEGER)
|
|
221
|
+
WHERE entries_fts MATCH ?
|
|
222
|
+
ORDER BY bm25Score
|
|
223
|
+
LIMIT ?
|
|
224
|
+
`;
|
|
225
|
+
params = [ftsQuery, limit];
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const rows = db.prepare(sql).all(...params);
|
|
229
|
+
return rows.map((row) => ({
|
|
230
|
+
id: row.id,
|
|
231
|
+
filePath: row.filePath,
|
|
232
|
+
entry: JSON.parse(row.entry_json),
|
|
233
|
+
searchText: row.searchText,
|
|
234
|
+
bm25Score: row.bm25Score,
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function sanitizeFtsQuery(query) {
|
|
242
|
+
const tokens = query
|
|
243
|
+
.replace(/[^a-zA-Z0-9\s]/g, " ")
|
|
244
|
+
.split(/\s+/)
|
|
245
|
+
.filter((t) => t.length > 1);
|
|
246
|
+
if (tokens.length === 0)
|
|
247
|
+
return "";
|
|
248
|
+
// Use unquoted tokens so the porter stemmer can normalize word forms
|
|
249
|
+
return tokens.join(" OR ");
|
|
250
|
+
}
|
|
251
|
+
// ── All entries ─────────────────────────────────────────────────────────────
|
|
252
|
+
export function getAllEntries(db, entryType) {
|
|
253
|
+
let sql;
|
|
254
|
+
let params;
|
|
255
|
+
if (entryType && entryType !== "any") {
|
|
256
|
+
sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE entry_type = ?";
|
|
257
|
+
params = [entryType];
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries";
|
|
261
|
+
params = [];
|
|
262
|
+
}
|
|
263
|
+
const rows = db.prepare(sql).all(...params);
|
|
264
|
+
return rows.map((row) => ({
|
|
265
|
+
id: row.id,
|
|
266
|
+
entryKey: row.entry_key,
|
|
267
|
+
dirPath: row.dir_path,
|
|
268
|
+
filePath: row.file_path,
|
|
269
|
+
stashDir: row.stash_dir,
|
|
270
|
+
entry: JSON.parse(row.entry_json),
|
|
271
|
+
searchText: row.search_text,
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
export function getEntryCount(db) {
|
|
275
|
+
const row = db.prepare("SELECT COUNT(*) AS cnt FROM entries").get();
|
|
276
|
+
return row.cnt;
|
|
277
|
+
}
|
|
278
|
+
export function getEntryById(db, id) {
|
|
279
|
+
const row = db
|
|
280
|
+
.prepare("SELECT file_path, entry_json FROM entries WHERE id = ?")
|
|
281
|
+
.get(id);
|
|
282
|
+
if (!row)
|
|
283
|
+
return undefined;
|
|
284
|
+
return { filePath: row.file_path, entry: JSON.parse(row.entry_json) };
|
|
285
|
+
}
|
|
286
|
+
export function getEntriesByDir(db, dirPath) {
|
|
287
|
+
const rows = db
|
|
288
|
+
.prepare("SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE dir_path = ?")
|
|
289
|
+
.all(dirPath);
|
|
290
|
+
return rows.map((row) => ({
|
|
291
|
+
id: row.id,
|
|
292
|
+
entryKey: row.entry_key,
|
|
293
|
+
dirPath: row.dir_path,
|
|
294
|
+
filePath: row.file_path,
|
|
295
|
+
stashDir: row.stash_dir,
|
|
296
|
+
entry: JSON.parse(row.entry_json),
|
|
297
|
+
searchText: row.search_text,
|
|
298
|
+
}));
|
|
299
|
+
}
|
package/dist/src/embedder.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { fetchWithTimeout } from "./common";
|
|
2
2
|
let localEmbedder;
|
|
3
3
|
async function getLocalEmbedder() {
|
|
4
4
|
if (!localEmbedder) {
|
|
@@ -10,7 +10,8 @@ async function getLocalEmbedder() {
|
|
|
10
10
|
catch {
|
|
11
11
|
throw new Error("Semantic search requires @xenova/transformers. Install it with: npm install @xenova/transformers");
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
const pipelineFn = pipeline;
|
|
14
|
+
localEmbedder = await pipelineFn("feature-extraction", "Xenova/all-MiniLM-L6-v2");
|
|
14
15
|
}
|
|
15
16
|
return localEmbedder;
|
|
16
17
|
}
|
|
@@ -25,13 +26,17 @@ async function embedRemote(text, config) {
|
|
|
25
26
|
if (config.apiKey) {
|
|
26
27
|
headers["Authorization"] = `Bearer ${config.apiKey}`;
|
|
27
28
|
}
|
|
28
|
-
const
|
|
29
|
+
const body = {
|
|
30
|
+
input: text,
|
|
31
|
+
model: config.model,
|
|
32
|
+
};
|
|
33
|
+
if (config.dimension) {
|
|
34
|
+
body.dimensions = config.dimension;
|
|
35
|
+
}
|
|
36
|
+
const response = await fetchWithTimeout(config.endpoint, {
|
|
29
37
|
method: "POST",
|
|
30
38
|
headers,
|
|
31
|
-
body: JSON.stringify(
|
|
32
|
-
input: text,
|
|
33
|
-
model: config.model,
|
|
34
|
-
}),
|
|
39
|
+
body: JSON.stringify(body),
|
|
35
40
|
});
|
|
36
41
|
if (!response.ok) {
|
|
37
42
|
const body = await response.text().catch(() => "");
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const GITHUB_API_BASE = "https://api.github.com";
|
|
2
|
+
export function githubHeaders() {
|
|
3
|
+
const token = process.env.GITHUB_TOKEN?.trim();
|
|
4
|
+
const headers = {
|
|
5
|
+
Accept: "application/vnd.github+json",
|
|
6
|
+
"User-Agent": "agentikit-registry",
|
|
7
|
+
};
|
|
8
|
+
if (token)
|
|
9
|
+
headers.Authorization = `Bearer ${token}`;
|
|
10
|
+
return headers;
|
|
11
|
+
}
|
|
12
|
+
export function asRecord(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
14
|
+
? value
|
|
15
|
+
: {};
|
|
16
|
+
}
|
|
17
|
+
export function asString(value) {
|
|
18
|
+
return typeof value === "string" && value ? value : undefined;
|
|
19
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { parseFrontmatter, toStringOrUndefined } from "../frontmatter";
|
|
2
|
+
import { isMarkdownFile, markdownCanonicalName, markdownAssetPath } from "./markdown-helpers";
|
|
3
|
+
export const agentHandler = {
|
|
4
|
+
typeName: "agent",
|
|
5
|
+
stashDir: "agents",
|
|
6
|
+
isRelevantFile: isMarkdownFile,
|
|
7
|
+
toCanonicalName: markdownCanonicalName,
|
|
8
|
+
toAssetPath: markdownAssetPath,
|
|
9
|
+
buildShowResponse(input) {
|
|
10
|
+
const parsedMd = parseFrontmatter(input.content);
|
|
11
|
+
return {
|
|
12
|
+
type: "agent",
|
|
13
|
+
name: input.name,
|
|
14
|
+
path: input.path,
|
|
15
|
+
description: toStringOrUndefined(parsedMd.data.description),
|
|
16
|
+
prompt: "Dispatching prompt must include the agent's full prompt content verbatim; summaries are non-compliant. \n\n"
|
|
17
|
+
+ parsedMd.content,
|
|
18
|
+
toolPolicy: parsedMd.data.tools,
|
|
19
|
+
modelHint: parsedMd.data.model,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
defaultUsageGuide: [
|
|
23
|
+
"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.",
|
|
24
|
+
"Use with `akm show <openRef>` to get the full prompt payload.",
|
|
25
|
+
],
|
|
26
|
+
};
|