agentikit 0.0.7 → 0.0.9
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 +215 -76
- package/dist/index.d.ts +17 -3
- package/dist/index.js +10 -2
- package/dist/src/asset-spec.d.ts +14 -0
- package/dist/src/asset-spec.js +46 -0
- package/dist/src/cli.js +268 -57
- package/dist/src/common.d.ts +8 -0
- package/dist/src/common.js +46 -0
- package/dist/src/config.d.ts +37 -0
- package/dist/src/config.js +124 -0
- package/dist/src/embedder.d.ts +10 -0
- package/dist/src/embedder.js +87 -0
- package/dist/src/frontmatter.d.ts +30 -0
- package/dist/src/frontmatter.js +86 -0
- package/dist/src/indexer.d.ts +20 -2
- package/dist/src/indexer.js +212 -80
- package/dist/src/init.d.ts +19 -0
- package/dist/src/init.js +87 -0
- package/dist/src/llm.d.ts +15 -0
- package/dist/src/llm.js +91 -0
- package/dist/src/markdown.d.ts +18 -0
- package/dist/src/markdown.js +77 -0
- package/dist/src/metadata.d.ts +11 -2
- package/dist/src/metadata.js +161 -29
- package/dist/src/registry-install.d.ts +11 -0
- package/dist/src/registry-install.js +208 -0
- package/dist/src/registry-resolve.d.ts +3 -0
- package/dist/src/registry-resolve.js +231 -0
- package/dist/src/registry-search.d.ts +5 -0
- package/dist/src/registry-search.js +129 -0
- package/dist/src/registry-types.d.ts +55 -0
- package/dist/src/registry-types.js +1 -0
- package/dist/src/ripgrep-install.d.ts +12 -0
- package/dist/src/ripgrep-install.js +169 -0
- package/dist/src/ripgrep-resolve.d.ts +13 -0
- package/dist/src/ripgrep-resolve.js +68 -0
- package/dist/src/ripgrep.d.ts +3 -36
- package/dist/src/ripgrep.js +2 -262
- package/dist/src/similarity.d.ts +1 -2
- package/dist/src/similarity.js +11 -0
- package/dist/src/stash-add.d.ts +4 -0
- package/dist/src/stash-add.js +59 -0
- package/dist/src/stash-ref.d.ts +7 -0
- package/dist/src/stash-ref.js +33 -0
- package/dist/src/stash-registry.d.ts +18 -0
- package/dist/src/stash-registry.js +221 -0
- package/dist/src/stash-resolve.d.ts +2 -0
- package/dist/src/stash-resolve.js +45 -0
- package/dist/src/stash-search.d.ts +8 -0
- package/dist/src/stash-search.js +484 -0
- package/dist/src/stash-show.d.ts +5 -0
- package/dist/src/stash-show.js +114 -0
- package/dist/src/stash-types.d.ts +217 -0
- package/dist/src/stash-types.js +1 -0
- package/dist/src/stash.d.ts +10 -63
- package/dist/src/stash.js +6 -633
- package/dist/src/tool-runner.d.ts +35 -0
- package/dist/src/tool-runner.js +100 -0
- package/dist/src/walker.d.ts +19 -0
- package/dist/src/walker.js +47 -0
- package/package.json +8 -14
- package/src/asset-spec.ts +69 -0
- package/src/cli.ts +282 -46
- package/src/common.ts +58 -0
- package/src/config.ts +183 -0
- package/src/embedder.ts +117 -0
- package/src/frontmatter.ts +95 -0
- package/src/indexer.ts +244 -84
- package/src/init.ts +106 -0
- package/src/llm.ts +124 -0
- package/src/markdown.ts +106 -0
- package/src/metadata.ts +171 -27
- package/src/registry-install.ts +245 -0
- package/src/registry-resolve.ts +272 -0
- package/src/registry-search.ts +145 -0
- package/src/registry-types.ts +64 -0
- package/src/ripgrep-install.ts +200 -0
- package/src/ripgrep-resolve.ts +72 -0
- package/src/ripgrep.ts +3 -315
- package/src/similarity.ts +13 -1
- package/src/stash-add.ts +66 -0
- package/src/stash-ref.ts +41 -0
- package/src/stash-registry.ts +259 -0
- package/src/stash-resolve.ts +47 -0
- package/src/stash-search.ts +595 -0
- package/src/stash-show.ts +112 -0
- package/src/stash-types.ts +221 -0
- package/src/stash.ts +31 -760
- package/src/tool-runner.ts +129 -0
- package/src/walker.ts +53 -0
- package/.claude-plugin/plugin.json +0 -21
- package/commands/open.md +0 -11
- package/commands/run.md +0 -11
- package/commands/search.md +0 -11
- package/dist/src/plugin.d.ts +0 -2
- package/dist/src/plugin.js +0 -55
- package/skills/stash/SKILL.md +0 -73
- package/src/plugin.ts +0 -56
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { hasErrnoCode, resolveStashDir } from "./common";
|
|
4
|
+
import { ASSET_TYPES, TYPE_DIRS, deriveCanonicalAssetName } from "./asset-spec";
|
|
5
|
+
import { loadSearchIndex, buildSearchText } from "./indexer";
|
|
6
|
+
import { TfIdfAdapter } from "./similarity";
|
|
7
|
+
import { buildToolInfo } from "./tool-runner";
|
|
8
|
+
import { walkStash } from "./walker";
|
|
9
|
+
import { makeOpenRef } from "./stash-ref";
|
|
10
|
+
import { loadConfig } from "./config";
|
|
11
|
+
import { searchRegistry } from "./registry-search";
|
|
12
|
+
const DEFAULT_LIMIT = 20;
|
|
13
|
+
const DEFAULT_USAGE_GUIDE_BY_TYPE = {
|
|
14
|
+
tool: [
|
|
15
|
+
"Use the hit's runCmd for execution so runtime and working directory stay correct.",
|
|
16
|
+
"Use `akm show <openRef>` to inspect the tool before running it.",
|
|
17
|
+
],
|
|
18
|
+
skill: [
|
|
19
|
+
"Read and apply the skill instructions as written, then adapt examples to your current repo state and task.",
|
|
20
|
+
"Use `akm show <openRef>` to read the full SKILL.md for required steps and constraints.",
|
|
21
|
+
],
|
|
22
|
+
command: [
|
|
23
|
+
"Read the .md file, fill placeholders, and run it in the current repo context.",
|
|
24
|
+
"Use `akm show <openRef>` to retrieve the command template body.",
|
|
25
|
+
],
|
|
26
|
+
agent: [
|
|
27
|
+
"Read the .md file and dispatch and agent using the content of the file. Use modelHint/toolPolicy when present to run the agent with compatible settings.",
|
|
28
|
+
"Use with `akm show <openRef>` to get the full prompt payload.",
|
|
29
|
+
],
|
|
30
|
+
knowledge: [
|
|
31
|
+
"Use `akm show <openRef>` to read the document; start with `--view toc` for large files.",
|
|
32
|
+
"Use `--view section` or `--view lines` to load only the part you need.",
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
export async function agentikitSearch(input) {
|
|
36
|
+
const t0 = Date.now();
|
|
37
|
+
const query = input.query.trim();
|
|
38
|
+
const normalizedQuery = query.toLowerCase();
|
|
39
|
+
const searchType = input.type ?? "any";
|
|
40
|
+
const limit = normalizeLimit(input.limit);
|
|
41
|
+
const usageMode = parseSearchUsageMode(input.usage);
|
|
42
|
+
const source = parseSearchSource(input.source);
|
|
43
|
+
const stashDir = resolveStashDir();
|
|
44
|
+
const localResult = source === "registry"
|
|
45
|
+
? undefined
|
|
46
|
+
: await searchLocal({
|
|
47
|
+
query: normalizedQuery,
|
|
48
|
+
searchType,
|
|
49
|
+
limit,
|
|
50
|
+
usageMode,
|
|
51
|
+
stashDir,
|
|
52
|
+
});
|
|
53
|
+
const registryResult = source === "local"
|
|
54
|
+
? undefined
|
|
55
|
+
: await searchRegistry(query, { limit });
|
|
56
|
+
if (source === "local") {
|
|
57
|
+
return {
|
|
58
|
+
stashDir,
|
|
59
|
+
source,
|
|
60
|
+
hits: localResult?.hits ?? [],
|
|
61
|
+
usageGuide: localResult?.usageGuide,
|
|
62
|
+
tip: localResult?.tip,
|
|
63
|
+
timing: { totalMs: Date.now() - t0, rankMs: localResult?.rankMs, embedMs: localResult?.embedMs },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const registryHits = (registryResult?.hits ?? []).map((hit) => {
|
|
67
|
+
const installRef = hit.source === "npm" ? `npm:${hit.ref}` : `github:${hit.ref}`;
|
|
68
|
+
return {
|
|
69
|
+
hitSource: "registry",
|
|
70
|
+
type: "registry",
|
|
71
|
+
name: hit.title,
|
|
72
|
+
id: hit.id,
|
|
73
|
+
registrySource: hit.source,
|
|
74
|
+
ref: hit.ref,
|
|
75
|
+
description: hit.description,
|
|
76
|
+
homepage: hit.homepage,
|
|
77
|
+
score: hit.score,
|
|
78
|
+
metadata: hit.metadata,
|
|
79
|
+
installRef,
|
|
80
|
+
installCmd: `akm add ${installRef}`,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
if (source === "registry") {
|
|
84
|
+
const hits = registryHits.slice(0, limit);
|
|
85
|
+
return {
|
|
86
|
+
stashDir,
|
|
87
|
+
source,
|
|
88
|
+
hits,
|
|
89
|
+
tip: hits.length === 0 ? "No matching registry entries were found." : undefined,
|
|
90
|
+
warnings: registryResult?.warnings.length ? registryResult.warnings : undefined,
|
|
91
|
+
timing: { totalMs: Date.now() - t0 },
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const mergedHits = mergeSearchHits(localResult?.hits ?? [], registryHits, limit);
|
|
95
|
+
return {
|
|
96
|
+
stashDir,
|
|
97
|
+
source,
|
|
98
|
+
hits: mergedHits,
|
|
99
|
+
usageGuide: localResult?.usageGuide,
|
|
100
|
+
tip: mergedHits.length === 0 ? "No matching stash assets or registry entries were found." : undefined,
|
|
101
|
+
warnings: registryResult?.warnings.length ? registryResult.warnings : undefined,
|
|
102
|
+
timing: { totalMs: Date.now() - t0 },
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function searchLocal(input) {
|
|
106
|
+
const { query, searchType, limit, usageMode, stashDir } = input;
|
|
107
|
+
const config = loadConfig(stashDir);
|
|
108
|
+
const allStashDirs = [
|
|
109
|
+
stashDir,
|
|
110
|
+
...config.additionalStashDirs.filter((d) => {
|
|
111
|
+
try {
|
|
112
|
+
return fs.statSync(d).isDirectory();
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}),
|
|
118
|
+
];
|
|
119
|
+
const index = loadSearchIndex();
|
|
120
|
+
if (index && index.entries && index.entries.length > 0 && index.stashDir === stashDir) {
|
|
121
|
+
const { hits, usageGuide, embedMs, rankMs } = await searchIndex(index, query, searchType, limit, stashDir, allStashDirs, config, usageMode);
|
|
122
|
+
return {
|
|
123
|
+
hits,
|
|
124
|
+
usageGuide,
|
|
125
|
+
tip: hits.length === 0 ? "No matching stash assets were found. Try running 'akm index' to rebuild." : undefined,
|
|
126
|
+
embedMs,
|
|
127
|
+
rankMs,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const hits = allStashDirs
|
|
131
|
+
.flatMap((dir) => substringSearch(query, searchType, limit, dir))
|
|
132
|
+
.slice(0, limit);
|
|
133
|
+
const usageGuide = shouldIncludeUsageGuide(usageMode) ? buildUsageGuide(hits.map((hit) => hit.type), searchType) : undefined;
|
|
134
|
+
return {
|
|
135
|
+
hits,
|
|
136
|
+
usageGuide,
|
|
137
|
+
tip: hits.length === 0 ? "No matching stash assets were found. Try running 'akm index' to rebuild." : undefined,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// ── Unified indexed search ──────────────────────────────────────────────────
|
|
141
|
+
async function searchIndex(index, query, searchType, limit, stashDir, allStashDirs, config, usageMode) {
|
|
142
|
+
// Filter candidates by type
|
|
143
|
+
let candidates = index.entries;
|
|
144
|
+
if (searchType !== "any") {
|
|
145
|
+
candidates = candidates.filter((ie) => ie.entry.type === searchType);
|
|
146
|
+
}
|
|
147
|
+
if (candidates.length === 0) {
|
|
148
|
+
return {
|
|
149
|
+
hits: [],
|
|
150
|
+
usageGuide: shouldIncludeUsageGuide(usageMode) ? buildUsageGuide([], searchType) : undefined,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
// Empty query: return all entries (no scoring needed)
|
|
154
|
+
if (!query) {
|
|
155
|
+
const selectedCandidates = candidates.slice(0, limit);
|
|
156
|
+
const hits = selectedCandidates.map((ie) => buildIndexedHit({
|
|
157
|
+
entry: ie.entry,
|
|
158
|
+
path: ie.path,
|
|
159
|
+
score: 1,
|
|
160
|
+
query,
|
|
161
|
+
rankingMode: "tfidf",
|
|
162
|
+
defaultStashDir: stashDir,
|
|
163
|
+
allStashDirs,
|
|
164
|
+
includeItemUsage: shouldIncludeItemUsage(usageMode),
|
|
165
|
+
}));
|
|
166
|
+
return {
|
|
167
|
+
hits,
|
|
168
|
+
usageGuide: shouldIncludeUsageGuide(usageMode)
|
|
169
|
+
? buildUsageGuideFromEntries(selectedCandidates.map((candidate) => candidate.entry), searchType)
|
|
170
|
+
: undefined,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
// Score each candidate using available signals
|
|
174
|
+
const tEmbed0 = Date.now();
|
|
175
|
+
const embeddingScores = await tryEmbeddingScores(candidates, query, config);
|
|
176
|
+
const embedMs = Date.now() - tEmbed0;
|
|
177
|
+
const tRank0 = Date.now();
|
|
178
|
+
const tfidfScores = computeTfidfScores(index, candidates, query, searchType);
|
|
179
|
+
const scored = [];
|
|
180
|
+
for (const ie of candidates) {
|
|
181
|
+
const key = ie.path;
|
|
182
|
+
const embScore = embeddingScores?.get(key);
|
|
183
|
+
const tfidfScore = tfidfScores.get(key) ?? 0;
|
|
184
|
+
if (embScore !== undefined) {
|
|
185
|
+
// Weighted blend: embedding dominates when available, TF-IDF boosts lexical matches
|
|
186
|
+
const blended = embScore * 0.7 + tfidfScore * 0.3;
|
|
187
|
+
if (blended > 0)
|
|
188
|
+
scored.push({ ie, score: blended, rankingMode: "semantic" });
|
|
189
|
+
}
|
|
190
|
+
else if (tfidfScore > 0) {
|
|
191
|
+
scored.push({ ie, score: tfidfScore, rankingMode: "tfidf" });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
scored.sort((a, b) => b.score - a.score);
|
|
195
|
+
const rankMs = Date.now() - tRank0;
|
|
196
|
+
const selected = scored.slice(0, limit);
|
|
197
|
+
const hits = selected.map(({ ie, score, rankingMode }) => buildIndexedHit({
|
|
198
|
+
entry: ie.entry,
|
|
199
|
+
path: ie.path,
|
|
200
|
+
score: Math.round(score * 1000) / 1000,
|
|
201
|
+
query,
|
|
202
|
+
rankingMode,
|
|
203
|
+
defaultStashDir: stashDir,
|
|
204
|
+
allStashDirs,
|
|
205
|
+
includeItemUsage: shouldIncludeItemUsage(usageMode),
|
|
206
|
+
}));
|
|
207
|
+
return {
|
|
208
|
+
embedMs,
|
|
209
|
+
rankMs,
|
|
210
|
+
hits,
|
|
211
|
+
usageGuide: shouldIncludeUsageGuide(usageMode)
|
|
212
|
+
? buildUsageGuideFromEntries(selected.map((item) => item.ie.entry), searchType)
|
|
213
|
+
: undefined,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
// ── Embedding scorer ────────────────────────────────────────────────────────
|
|
217
|
+
async function tryEmbeddingScores(candidates, query, config) {
|
|
218
|
+
if (!config.semanticSearch)
|
|
219
|
+
return null;
|
|
220
|
+
const withEmbeddings = candidates.filter((ie) => ie.embedding && ie.embedding.length > 0);
|
|
221
|
+
if (withEmbeddings.length === 0)
|
|
222
|
+
return null;
|
|
223
|
+
try {
|
|
224
|
+
const { embed, cosineSimilarity } = await import("./embedder.js");
|
|
225
|
+
const queryEmbedding = await embed(query, config.embedding);
|
|
226
|
+
const scores = new Map();
|
|
227
|
+
for (const ie of withEmbeddings) {
|
|
228
|
+
scores.set(ie.path, cosineSimilarity(queryEmbedding, ie.embedding));
|
|
229
|
+
}
|
|
230
|
+
return scores;
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// ── TF-IDF scorer ───────────────────────────────────────────────────────────
|
|
237
|
+
function computeTfidfScores(index, candidates, query, searchType) {
|
|
238
|
+
const candidateScoredEntries = toScoredEntries(candidates);
|
|
239
|
+
let adapter;
|
|
240
|
+
if (index.tfidf) {
|
|
241
|
+
const allScored = toScoredEntries(index.entries);
|
|
242
|
+
adapter = TfIdfAdapter.deserialize(index.tfidf, allScored);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
adapter = new TfIdfAdapter();
|
|
246
|
+
adapter.buildIndex(candidateScoredEntries);
|
|
247
|
+
}
|
|
248
|
+
const typeFilter = searchType === "any" ? undefined : searchType;
|
|
249
|
+
const results = adapter.search(query, candidates.length, typeFilter);
|
|
250
|
+
const scores = new Map();
|
|
251
|
+
for (const r of results) {
|
|
252
|
+
scores.set(r.path, r.score);
|
|
253
|
+
}
|
|
254
|
+
return scores;
|
|
255
|
+
}
|
|
256
|
+
// ── Substring fallback (no index) ───────────────────────────────────────────
|
|
257
|
+
function substringSearch(query, searchType, limit, stashDir) {
|
|
258
|
+
const assets = indexAssets(stashDir, searchType);
|
|
259
|
+
return assets
|
|
260
|
+
.filter((asset) => asset.name.toLowerCase().includes(query))
|
|
261
|
+
.sort(compareAssets)
|
|
262
|
+
.slice(0, limit)
|
|
263
|
+
.map((asset) => assetToSearchHit(asset, stashDir));
|
|
264
|
+
}
|
|
265
|
+
// ── Hit building ────────────────────────────────────────────────────────────
|
|
266
|
+
function findStashDirForPath(filePath, stashDirs) {
|
|
267
|
+
const resolved = path.resolve(filePath);
|
|
268
|
+
for (const dir of stashDirs) {
|
|
269
|
+
if (resolved.startsWith(path.resolve(dir) + path.sep))
|
|
270
|
+
return dir;
|
|
271
|
+
}
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
function buildIndexedHit(input) {
|
|
275
|
+
const entryStashDir = findStashDirForPath(input.path, input.allStashDirs) ?? input.defaultStashDir;
|
|
276
|
+
const typeRoot = path.join(entryStashDir, TYPE_DIRS[input.entry.type]);
|
|
277
|
+
const openRefName = deriveCanonicalAssetName(input.entry.type, typeRoot, input.path)
|
|
278
|
+
?? input.entry.name;
|
|
279
|
+
const qualityBoost = input.entry.generated === true ? 0 : 0.05;
|
|
280
|
+
const confidenceBoost = typeof input.entry.confidence === "number" ? Math.min(0.05, Math.max(0, input.entry.confidence) * 0.05) : 0;
|
|
281
|
+
const score = Math.round((input.score + qualityBoost + confidenceBoost) * 1000) / 1000;
|
|
282
|
+
const whyMatched = buildWhyMatched(input.entry, input.query, input.rankingMode, qualityBoost, confidenceBoost);
|
|
283
|
+
const hit = {
|
|
284
|
+
hitSource: "local",
|
|
285
|
+
type: input.entry.type,
|
|
286
|
+
name: input.entry.name,
|
|
287
|
+
path: input.path,
|
|
288
|
+
openRef: makeOpenRef(input.entry.type, openRefName),
|
|
289
|
+
description: input.entry.description,
|
|
290
|
+
tags: input.entry.tags,
|
|
291
|
+
score,
|
|
292
|
+
whyMatched,
|
|
293
|
+
};
|
|
294
|
+
if (input.includeItemUsage && input.entry.usage && input.entry.usage.length > 0) {
|
|
295
|
+
hit.usage = input.entry.usage;
|
|
296
|
+
}
|
|
297
|
+
if (input.entry.type === "tool") {
|
|
298
|
+
try {
|
|
299
|
+
const toolInfo = buildToolInfo(entryStashDir, input.path);
|
|
300
|
+
hit.runCmd = toolInfo.runCmd;
|
|
301
|
+
hit.kind = toolInfo.kind;
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
if (!hasErrnoCode(error, "ENOENT"))
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return hit;
|
|
309
|
+
}
|
|
310
|
+
function buildWhyMatched(entry, query, rankingMode, qualityBoost, confidenceBoost) {
|
|
311
|
+
const reasons = [rankingMode === "semantic" ? "semantic similarity" : "tf-idf lexical relevance"];
|
|
312
|
+
const tokens = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
313
|
+
const name = entry.name.toLowerCase();
|
|
314
|
+
const tags = entry.tags?.join(" ").toLowerCase() ?? "";
|
|
315
|
+
const intents = entry.intents?.join(" ").toLowerCase() ?? "";
|
|
316
|
+
const aliases = entry.aliases?.join(" ").toLowerCase() ?? "";
|
|
317
|
+
if (tokens.some((t) => name.includes(t)))
|
|
318
|
+
reasons.push("matched name tokens");
|
|
319
|
+
if (tokens.some((t) => tags.includes(t)))
|
|
320
|
+
reasons.push("matched tags");
|
|
321
|
+
if (tokens.some((t) => intents.includes(t)))
|
|
322
|
+
reasons.push("matched intents");
|
|
323
|
+
if (tokens.some((t) => aliases.includes(t)))
|
|
324
|
+
reasons.push("matched aliases");
|
|
325
|
+
if (qualityBoost > 0)
|
|
326
|
+
reasons.push("curated metadata boost");
|
|
327
|
+
if (confidenceBoost > 0)
|
|
328
|
+
reasons.push("metadata confidence boost");
|
|
329
|
+
return reasons;
|
|
330
|
+
}
|
|
331
|
+
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
332
|
+
function toScoredEntries(entries) {
|
|
333
|
+
return entries.map((ie) => ({
|
|
334
|
+
id: `${ie.entry.type}:${ie.entry.name}`,
|
|
335
|
+
text: buildSearchText(ie.entry),
|
|
336
|
+
entry: ie.entry,
|
|
337
|
+
path: ie.path,
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
function assetToSearchHit(asset, stashDir) {
|
|
341
|
+
if (asset.type !== "tool") {
|
|
342
|
+
return {
|
|
343
|
+
hitSource: "local",
|
|
344
|
+
type: asset.type,
|
|
345
|
+
name: asset.name,
|
|
346
|
+
path: asset.path,
|
|
347
|
+
openRef: makeOpenRef(asset.type, asset.name),
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
const toolInfo = buildToolInfo(stashDir, asset.path);
|
|
351
|
+
return {
|
|
352
|
+
hitSource: "local",
|
|
353
|
+
type: "tool",
|
|
354
|
+
name: asset.name,
|
|
355
|
+
path: asset.path,
|
|
356
|
+
openRef: makeOpenRef("tool", asset.name),
|
|
357
|
+
runCmd: toolInfo.runCmd,
|
|
358
|
+
kind: toolInfo.kind,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
function normalizeLimit(limit) {
|
|
362
|
+
if (typeof limit !== "number" || Number.isNaN(limit) || limit <= 0) {
|
|
363
|
+
return DEFAULT_LIMIT;
|
|
364
|
+
}
|
|
365
|
+
return Math.min(Math.floor(limit), 200);
|
|
366
|
+
}
|
|
367
|
+
function parseSearchUsageMode(mode) {
|
|
368
|
+
if (mode === "none" || mode === "both" || mode === "item" || mode === "guide") {
|
|
369
|
+
return mode;
|
|
370
|
+
}
|
|
371
|
+
if (typeof mode === "undefined")
|
|
372
|
+
return "both";
|
|
373
|
+
throw new Error(`Invalid usage mode: ${String(mode)}. Expected one of: none|both|item|guide`);
|
|
374
|
+
}
|
|
375
|
+
function parseSearchSource(source) {
|
|
376
|
+
if (source === "local" || source === "registry" || source === "both")
|
|
377
|
+
return source;
|
|
378
|
+
if (typeof source === "undefined")
|
|
379
|
+
return "local";
|
|
380
|
+
throw new Error(`Invalid search source: ${String(source)}. Expected one of: local|registry|both`);
|
|
381
|
+
}
|
|
382
|
+
function mergeSearchHits(localHits, registryHits, limit) {
|
|
383
|
+
const merged = [];
|
|
384
|
+
let localIndex = 0;
|
|
385
|
+
let registryIndex = 0;
|
|
386
|
+
while (merged.length < limit && (localIndex < localHits.length || registryIndex < registryHits.length)) {
|
|
387
|
+
if (localIndex < localHits.length) {
|
|
388
|
+
merged.push(localHits[localIndex]);
|
|
389
|
+
localIndex += 1;
|
|
390
|
+
if (merged.length >= limit)
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
if (registryIndex < registryHits.length) {
|
|
394
|
+
merged.push(registryHits[registryIndex]);
|
|
395
|
+
registryIndex += 1;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return merged;
|
|
399
|
+
}
|
|
400
|
+
function shouldIncludeUsageGuide(mode) {
|
|
401
|
+
return mode === "both" || mode === "guide";
|
|
402
|
+
}
|
|
403
|
+
function shouldIncludeItemUsage(mode) {
|
|
404
|
+
return mode === "both" || mode === "item";
|
|
405
|
+
}
|
|
406
|
+
function buildUsageGuideFromEntries(entries, searchType) {
|
|
407
|
+
const types = entries.map((entry) => entry.type);
|
|
408
|
+
const fallbackGuide = buildUsageGuide(types, searchType);
|
|
409
|
+
const metadataByType = new Map();
|
|
410
|
+
for (const entry of entries) {
|
|
411
|
+
if (!entry.usage || entry.usage.length === 0)
|
|
412
|
+
continue;
|
|
413
|
+
const current = metadataByType.get(entry.type) ?? [];
|
|
414
|
+
for (const item of entry.usage) {
|
|
415
|
+
const trimmed = item.trim();
|
|
416
|
+
if (trimmed && !current.includes(trimmed))
|
|
417
|
+
current.push(trimmed);
|
|
418
|
+
}
|
|
419
|
+
if (current.length > 0)
|
|
420
|
+
metadataByType.set(entry.type, current);
|
|
421
|
+
}
|
|
422
|
+
if (!fallbackGuide && metadataByType.size === 0)
|
|
423
|
+
return undefined;
|
|
424
|
+
const result = {};
|
|
425
|
+
for (const assetType of resolveGuideTypes(types, searchType)) {
|
|
426
|
+
const lines = [];
|
|
427
|
+
const metadataLines = metadataByType.get(assetType);
|
|
428
|
+
if (metadataLines && metadataLines.length > 0) {
|
|
429
|
+
lines.push(...metadataLines);
|
|
430
|
+
}
|
|
431
|
+
const fallbackLines = fallbackGuide?.[assetType];
|
|
432
|
+
if (fallbackLines && fallbackLines.length > 0) {
|
|
433
|
+
for (const line of fallbackLines) {
|
|
434
|
+
if (!lines.includes(line))
|
|
435
|
+
lines.push(line);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (lines.length > 0)
|
|
439
|
+
result[assetType] = lines;
|
|
440
|
+
}
|
|
441
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
442
|
+
}
|
|
443
|
+
function buildUsageGuide(hitTypes, searchType) {
|
|
444
|
+
const result = {};
|
|
445
|
+
for (const assetType of resolveGuideTypes(hitTypes, searchType)) {
|
|
446
|
+
result[assetType] = usageGuideByType(assetType);
|
|
447
|
+
}
|
|
448
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
449
|
+
}
|
|
450
|
+
function resolveGuideTypes(hitTypes, searchType) {
|
|
451
|
+
if (searchType !== "any")
|
|
452
|
+
return [searchType];
|
|
453
|
+
return Array.from(new Set(hitTypes));
|
|
454
|
+
}
|
|
455
|
+
function usageGuideByType(type) {
|
|
456
|
+
return DEFAULT_USAGE_GUIDE_BY_TYPE[type];
|
|
457
|
+
}
|
|
458
|
+
function fileToAsset(assetType, root, file) {
|
|
459
|
+
const name = deriveCanonicalAssetName(assetType, root, file);
|
|
460
|
+
if (!name)
|
|
461
|
+
return undefined;
|
|
462
|
+
return { type: assetType, name, path: file };
|
|
463
|
+
}
|
|
464
|
+
function indexAssets(stashDir, type) {
|
|
465
|
+
const assets = [];
|
|
466
|
+
const types = type === "any" ? ASSET_TYPES : [type];
|
|
467
|
+
for (const assetType of types) {
|
|
468
|
+
const root = path.join(stashDir, TYPE_DIRS[assetType]);
|
|
469
|
+
const groups = walkStash(root, assetType);
|
|
470
|
+
for (const { files } of groups) {
|
|
471
|
+
for (const file of files) {
|
|
472
|
+
const asset = fileToAsset(assetType, root, file);
|
|
473
|
+
if (asset)
|
|
474
|
+
assets.push(asset);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return assets;
|
|
479
|
+
}
|
|
480
|
+
function compareAssets(a, b) {
|
|
481
|
+
if (a.type !== b.type)
|
|
482
|
+
return a.type.localeCompare(b.type);
|
|
483
|
+
return a.name.localeCompare(b.name);
|
|
484
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseFrontmatter, toStringOrUndefined } from "./frontmatter";
|
|
4
|
+
import { resolveStashDir } from "./common";
|
|
5
|
+
import { parseOpenRef } from "./stash-ref";
|
|
6
|
+
import { resolveAssetPath } from "./stash-resolve";
|
|
7
|
+
import { parseMarkdownToc, extractSection, extractLineRange, extractFrontmatterOnly, formatToc } from "./markdown";
|
|
8
|
+
import { buildToolInfo } from "./tool-runner";
|
|
9
|
+
import { loadConfig } from "./config";
|
|
10
|
+
export function agentikitShow(input) {
|
|
11
|
+
const parsed = parseOpenRef(input.ref);
|
|
12
|
+
const stashDir = resolveStashDir();
|
|
13
|
+
const config = loadConfig(stashDir);
|
|
14
|
+
const allStashDirs = [
|
|
15
|
+
stashDir,
|
|
16
|
+
...config.additionalStashDirs.filter((d) => {
|
|
17
|
+
try {
|
|
18
|
+
return fs.statSync(d).isDirectory();
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
];
|
|
25
|
+
let assetPath;
|
|
26
|
+
let lastError;
|
|
27
|
+
for (const dir of allStashDirs) {
|
|
28
|
+
try {
|
|
29
|
+
assetPath = resolveAssetPath(dir, parsed.type, parsed.name);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (!assetPath) {
|
|
37
|
+
throw lastError ?? new Error(`Stash asset not found for ref: ${parsed.type}:${parsed.name}`);
|
|
38
|
+
}
|
|
39
|
+
const content = fs.readFileSync(assetPath, "utf8");
|
|
40
|
+
switch (parsed.type) {
|
|
41
|
+
case "skill":
|
|
42
|
+
return {
|
|
43
|
+
type: "skill",
|
|
44
|
+
name: parsed.name,
|
|
45
|
+
path: assetPath,
|
|
46
|
+
content,
|
|
47
|
+
};
|
|
48
|
+
case "command": {
|
|
49
|
+
const parsedMd = parseFrontmatter(content);
|
|
50
|
+
return {
|
|
51
|
+
type: "command",
|
|
52
|
+
name: parsed.name,
|
|
53
|
+
path: assetPath,
|
|
54
|
+
description: toStringOrUndefined(parsedMd.data.description),
|
|
55
|
+
template: parsedMd.content,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
case "agent": {
|
|
59
|
+
const parsedMd = parseFrontmatter(content);
|
|
60
|
+
return {
|
|
61
|
+
type: "agent",
|
|
62
|
+
name: parsed.name,
|
|
63
|
+
path: assetPath,
|
|
64
|
+
description: toStringOrUndefined(parsedMd.data.description),
|
|
65
|
+
prompt: "Dispatching prompt must include the agent's full prompt content verbatim; summaries are non-compliant. \n\n"
|
|
66
|
+
+ parsedMd.content,
|
|
67
|
+
toolPolicy: parsedMd.data.tools,
|
|
68
|
+
modelHint: parsedMd.data.model,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
case "tool": {
|
|
72
|
+
const assetStashDir = allStashDirs.find((d) => path.resolve(assetPath).startsWith(path.resolve(d) + path.sep)) ?? stashDir;
|
|
73
|
+
const toolInfo = buildToolInfo(assetStashDir, assetPath);
|
|
74
|
+
return {
|
|
75
|
+
type: "tool",
|
|
76
|
+
name: parsed.name,
|
|
77
|
+
path: assetPath,
|
|
78
|
+
runCmd: toolInfo.runCmd,
|
|
79
|
+
kind: toolInfo.kind,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
case "knowledge": {
|
|
83
|
+
const v = input.view ?? { mode: "full" };
|
|
84
|
+
switch (v.mode) {
|
|
85
|
+
case "toc": {
|
|
86
|
+
const toc = parseMarkdownToc(content);
|
|
87
|
+
return { type: "knowledge", name: parsed.name, path: assetPath, content: formatToc(toc) };
|
|
88
|
+
}
|
|
89
|
+
case "frontmatter": {
|
|
90
|
+
const fm = extractFrontmatterOnly(content);
|
|
91
|
+
return { type: "knowledge", name: parsed.name, path: assetPath, content: fm ?? "(no frontmatter)" };
|
|
92
|
+
}
|
|
93
|
+
case "section": {
|
|
94
|
+
const section = extractSection(content, v.heading);
|
|
95
|
+
if (!section) {
|
|
96
|
+
return {
|
|
97
|
+
type: "knowledge",
|
|
98
|
+
name: parsed.name,
|
|
99
|
+
path: assetPath,
|
|
100
|
+
content: `Section "${v.heading}" not found in ${parsed.name}. Try --view toc to discover available headings.`,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return { type: "knowledge", name: parsed.name, path: assetPath, content: section.content };
|
|
104
|
+
}
|
|
105
|
+
case "lines": {
|
|
106
|
+
return { type: "knowledge", name: parsed.name, path: assetPath, content: extractLineRange(content, v.start, v.end) };
|
|
107
|
+
}
|
|
108
|
+
default: {
|
|
109
|
+
return { type: "knowledge", name: parsed.name, path: assetPath, content };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|