akm-cli 0.7.5 → 0.8.0-rc.3
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/.github/CHANGELOG.md +1 -1
- package/dist/cli/parse-args.js +86 -0
- package/dist/cli.js +1023 -521
- package/dist/commands/agent-dispatch.js +107 -0
- package/dist/commands/agent-support.js +62 -0
- package/dist/commands/config-cli.js +68 -84
- package/dist/commands/consolidate.js +812 -0
- package/dist/commands/distill-promotion-policy.js +658 -0
- package/dist/commands/distill.js +218 -43
- package/dist/commands/eval-cases.js +40 -0
- package/dist/commands/events.js +2 -23
- package/dist/commands/graph.js +222 -0
- package/dist/commands/health.js +376 -0
- package/dist/commands/help/help-accept.md +9 -0
- package/dist/commands/help/help-improve.md +53 -0
- package/dist/commands/help/help-proposals.md +15 -0
- package/dist/commands/help/help-propose.md +17 -0
- package/dist/commands/help/help-reject.md +8 -0
- package/dist/commands/history.js +3 -30
- package/dist/commands/improve.js +1161 -0
- package/dist/commands/info.js +2 -2
- package/dist/commands/init.js +2 -2
- package/dist/commands/install-audit.js +5 -1
- package/dist/commands/installed-stashes.js +118 -138
- package/dist/commands/knowledge.js +133 -0
- package/dist/commands/lint/agent-linter.js +46 -0
- package/dist/commands/lint/base-linter.js +291 -0
- package/dist/commands/lint/command-linter.js +46 -0
- package/dist/commands/lint/default-linter.js +13 -0
- package/dist/commands/lint/index.js +145 -0
- package/dist/commands/lint/knowledge-linter.js +13 -0
- package/dist/commands/lint/memory-linter.js +58 -0
- package/dist/commands/lint/registry.js +33 -0
- package/dist/commands/lint/skill-linter.js +42 -0
- package/dist/commands/lint/task-linter.js +47 -0
- package/dist/commands/lint/types.js +1 -0
- package/dist/commands/lint/vault-key-rules.js +67 -0
- package/dist/commands/lint/workflow-linter.js +53 -0
- package/dist/commands/lint.js +1 -0
- package/dist/commands/proposal.js +8 -7
- package/dist/commands/propose.js +71 -28
- package/dist/commands/reflect.js +135 -35
- package/dist/commands/registry-search.js +2 -2
- package/dist/commands/remember.js +54 -0
- package/dist/commands/schema-repair.js +130 -0
- package/dist/commands/search.js +21 -5
- package/dist/commands/show.js +125 -20
- package/dist/commands/source-add.js +10 -10
- package/dist/commands/source-manage.js +11 -19
- package/dist/commands/tasks.js +385 -0
- package/dist/commands/url-checker.js +39 -0
- package/dist/commands/vault.js +168 -77
- package/dist/core/action-contributors.js +25 -0
- package/dist/core/asset-ref.js +4 -0
- package/dist/core/asset-registry.js +4 -16
- package/dist/core/asset-spec.js +10 -0
- package/dist/core/common.js +100 -0
- package/dist/core/concurrent.js +22 -0
- package/dist/core/config.js +233 -133
- package/dist/core/events.js +73 -126
- package/dist/core/frontmatter.js +0 -6
- package/dist/core/markdown.js +17 -0
- package/dist/core/memory-improve.js +678 -0
- package/dist/core/parse.js +155 -0
- package/dist/core/paths.js +101 -3
- package/dist/core/proposal-validators.js +61 -0
- package/dist/core/proposals.js +49 -38
- package/dist/core/state-db.js +731 -0
- package/dist/core/time.js +51 -0
- package/dist/core/warn.js +59 -1
- package/dist/indexer/db-search.js +52 -238
- package/dist/indexer/db.js +403 -54
- package/dist/indexer/ensure-index.js +61 -0
- package/dist/indexer/graph-boost.js +247 -94
- package/dist/indexer/graph-db.js +201 -0
- package/dist/indexer/graph-dedup.js +99 -0
- package/dist/indexer/graph-extraction.js +409 -76
- package/dist/indexer/index-context.js +10 -0
- package/dist/indexer/indexer.js +456 -290
- package/dist/indexer/llm-cache.js +47 -0
- package/dist/indexer/matchers.js +124 -160
- package/dist/indexer/memory-inference.js +63 -29
- package/dist/indexer/metadata-contributors.js +26 -0
- package/dist/indexer/metadata.js +196 -197
- package/dist/indexer/path-resolver.js +89 -0
- package/dist/indexer/ranking-contributors.js +204 -0
- package/dist/indexer/ranking.js +74 -0
- package/dist/indexer/search-hit-enrichers.js +22 -0
- package/dist/indexer/search-source.js +24 -9
- package/dist/indexer/semantic-status.js +2 -16
- package/dist/indexer/walker.js +25 -0
- package/dist/integrations/agent/builders.js +109 -0
- package/dist/integrations/agent/config.js +203 -3
- package/dist/integrations/agent/index.js +5 -2
- package/dist/integrations/agent/model-aliases.js +63 -0
- package/dist/integrations/agent/profiles.js +67 -5
- package/dist/integrations/agent/prompts.js +77 -72
- package/dist/integrations/agent/sdk-runner.js +120 -0
- package/dist/integrations/agent/spawn.js +93 -22
- package/dist/integrations/lockfile.js +10 -18
- package/dist/integrations/session-logs/index.js +65 -0
- package/dist/integrations/session-logs/providers/claude-code.js +56 -0
- package/dist/integrations/session-logs/providers/opencode.js +52 -0
- package/dist/integrations/session-logs/types.js +1 -0
- package/dist/llm/call-ai.js +74 -0
- package/dist/llm/client.js +61 -122
- package/dist/llm/feature-gate.js +27 -16
- package/dist/llm/graph-extract.js +297 -62
- package/dist/llm/memory-infer.js +49 -71
- package/dist/llm/metadata-enhance.js +39 -22
- package/dist/llm/prompts/graph-extract-user-prompt.md +12 -0
- package/dist/output/cli-hints-full.md +277 -0
- package/dist/output/cli-hints-short.md +65 -0
- package/dist/output/cli-hints.js +2 -318
- package/dist/output/renderers.js +220 -256
- package/dist/output/shapes.js +101 -93
- package/dist/output/text.js +256 -17
- package/dist/registry/providers/skills-sh.js +61 -49
- package/dist/registry/providers/static-index.js +44 -48
- package/dist/registry/resolve.js +8 -16
- package/dist/setup/setup.js +510 -11
- package/dist/sources/provider-factory.js +2 -1
- package/dist/sources/providers/filesystem.js +16 -23
- package/dist/sources/providers/git.js +4 -5
- package/dist/sources/providers/website.js +15 -22
- package/dist/sources/website-ingest.js +4 -0
- package/dist/tasks/backends/cron.js +200 -0
- package/dist/tasks/backends/exec-utils.js +25 -0
- package/dist/tasks/backends/index.js +32 -0
- package/dist/tasks/backends/launchd-template.xml +19 -0
- package/dist/tasks/backends/launchd.js +184 -0
- package/dist/tasks/backends/schtasks-template.xml +29 -0
- package/dist/tasks/backends/schtasks.js +212 -0
- package/dist/tasks/parser.js +198 -0
- package/dist/tasks/resolveAkmBin.js +84 -0
- package/dist/tasks/runner.js +432 -0
- package/dist/tasks/schedule.js +208 -0
- package/dist/tasks/schema.js +13 -0
- package/dist/tasks/validator.js +59 -0
- package/dist/wiki/index-template.md +12 -0
- package/dist/wiki/ingest-workflow-template.md +54 -0
- package/dist/wiki/log-template.md +8 -0
- package/dist/wiki/schema-template.md +61 -0
- package/dist/wiki/wiki-templates.js +12 -0
- package/dist/wiki/wiki.js +10 -61
- package/dist/workflows/authoring.js +5 -25
- package/dist/workflows/renderer.js +8 -3
- package/dist/workflows/runs.js +59 -91
- package/dist/workflows/validator.js +1 -1
- package/dist/workflows/workflow-template.md +24 -0
- package/docs/README.md +5 -2
- package/docs/migration/release-notes/0.7.0.md +1 -1
- package/docs/migration/release-notes/0.8.0.md +43 -0
- package/package.json +3 -2
- package/dist/templates/wiki-templates.js +0 -100
package/dist/indexer/metadata.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { deriveCanonicalAssetName, deriveCanonicalAssetNameFromStashRoot, isRelevantAssetFile, } from "../core/asset-spec";
|
|
4
|
-
import { isAssetType } from "../core/common";
|
|
5
|
-
import { parseFrontmatter
|
|
4
|
+
import { asNonEmptyString, isAssetType, writeFileAtomic } from "../core/common";
|
|
5
|
+
import { parseFrontmatter } from "../core/frontmatter";
|
|
6
6
|
import { isVerbose, warn } from "../core/warn";
|
|
7
7
|
import { buildFileContext, buildRenderContext, getRenderer, runMatchers } from "./file-context";
|
|
8
|
+
import { applyMetadataContributors } from "./metadata-contributors";
|
|
8
9
|
export const SCOPE_KEYS = ["user", "agent", "run", "channel"];
|
|
9
10
|
// ── Load / Write ────────────────────────────────────────────────────────────
|
|
10
11
|
const STASH_FILENAME = ".stash.json";
|
|
11
12
|
// ── Quality semantics (v1 spec §4.2) ────────────────────────────────────────
|
|
12
13
|
/**
|
|
13
|
-
* Well-known quality values. `generated` and `
|
|
14
|
+
* Well-known quality values. `generated`, `curated`, and `enriched` are included in
|
|
14
15
|
* default search; `proposed` is excluded by default and opt-in via
|
|
15
16
|
* `--include-proposed`. Unknown values warn once and remain searchable.
|
|
16
17
|
*/
|
|
17
|
-
export const KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "proposed"]);
|
|
18
|
+
export const KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "enriched", "proposed"]);
|
|
18
19
|
/** Tracks unknown quality values we've already warned about (one warn per value per process). */
|
|
19
20
|
const warnedUnknownQualityValues = new Set();
|
|
20
21
|
/**
|
|
@@ -80,20 +81,7 @@ export function loadStashFile(dirPath, options) {
|
|
|
80
81
|
}
|
|
81
82
|
export function writeStashFile(dirPath, stash) {
|
|
82
83
|
const filePath = stashFilePath(dirPath);
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
fs.writeFileSync(tmpPath, `${JSON.stringify(stash, null, 2)}\n`, "utf8");
|
|
86
|
-
fs.renameSync(tmpPath, filePath);
|
|
87
|
-
}
|
|
88
|
-
catch (err) {
|
|
89
|
-
try {
|
|
90
|
-
fs.unlinkSync(tmpPath);
|
|
91
|
-
}
|
|
92
|
-
catch {
|
|
93
|
-
/* ignore cleanup failure */
|
|
94
|
-
}
|
|
95
|
-
throw err;
|
|
96
|
-
}
|
|
84
|
+
writeFileAtomic(filePath, `${JSON.stringify(stash, null, 2)}\n`);
|
|
97
85
|
}
|
|
98
86
|
/**
|
|
99
87
|
* Validate and normalize a raw object into a `StashEntry`.
|
|
@@ -185,20 +173,24 @@ export function validateStashEntry(entry) {
|
|
|
185
173
|
if (typeof e.pageKind === "string" && e.pageKind.trim().length > 0) {
|
|
186
174
|
result.pageKind = e.pageKind.trim();
|
|
187
175
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const filtered = e.sources
|
|
197
|
-
.filter((s) => typeof s === "string" && s.trim().length > 0)
|
|
198
|
-
.map((s) => s.trim());
|
|
199
|
-
if (filtered.length > 0)
|
|
200
|
-
result.sources = filtered;
|
|
176
|
+
const xrefs = normalizeNonEmptyStringList(e.xrefs);
|
|
177
|
+
if (xrefs)
|
|
178
|
+
result.xrefs = xrefs;
|
|
179
|
+
const sources = normalizeNonEmptyStringList(e.sources);
|
|
180
|
+
if (sources)
|
|
181
|
+
result.sources = sources;
|
|
182
|
+
if (typeof e.beliefState === "string" && e.beliefState.trim().length > 0) {
|
|
183
|
+
result.beliefState = e.beliefState.trim();
|
|
201
184
|
}
|
|
185
|
+
const supersededBy = normalizeNonEmptyStringList(e.supersededBy);
|
|
186
|
+
if (supersededBy)
|
|
187
|
+
result.supersededBy = supersededBy;
|
|
188
|
+
const contradictedBy = normalizeNonEmptyStringList(e.contradictedBy);
|
|
189
|
+
if (contradictedBy)
|
|
190
|
+
result.contradictedBy = contradictedBy;
|
|
191
|
+
const currentBeliefRefs = normalizeNonEmptyStringList(e.currentBeliefRefs);
|
|
192
|
+
if (currentBeliefRefs)
|
|
193
|
+
result.currentBeliefRefs = currentBeliefRefs;
|
|
202
194
|
if (typeof e.scope === "object" && e.scope !== null && !Array.isArray(e.scope)) {
|
|
203
195
|
const scope = normalizeScopeObject(e.scope);
|
|
204
196
|
if (scope)
|
|
@@ -275,9 +267,9 @@ function normalizeIntent(value) {
|
|
|
275
267
|
return undefined;
|
|
276
268
|
const raw = value;
|
|
277
269
|
const intent = {};
|
|
278
|
-
const when =
|
|
279
|
-
const input =
|
|
280
|
-
const output =
|
|
270
|
+
const when = asNonEmptyString(raw.when);
|
|
271
|
+
const input = asNonEmptyString(raw.input);
|
|
272
|
+
const output = asNonEmptyString(raw.output);
|
|
281
273
|
if (when)
|
|
282
274
|
intent.when = when;
|
|
283
275
|
if (input)
|
|
@@ -290,7 +282,7 @@ function normalizeStringListOrUndefined(value) {
|
|
|
290
282
|
return normalizeNonEmptyStringList(value);
|
|
291
283
|
}
|
|
292
284
|
export function applyCuratedFrontmatter(entry, fmData) {
|
|
293
|
-
const description =
|
|
285
|
+
const description = asNonEmptyString(fmData.description);
|
|
294
286
|
if (description) {
|
|
295
287
|
entry.description = description;
|
|
296
288
|
entry.source = "frontmatter";
|
|
@@ -311,18 +303,30 @@ export function applyCuratedFrontmatter(entry, fmData) {
|
|
|
311
303
|
const examples = normalizeStringListOrUndefined(fmData.examples);
|
|
312
304
|
if (examples)
|
|
313
305
|
entry.examples = examples;
|
|
314
|
-
const run =
|
|
306
|
+
const run = asNonEmptyString(fmData.run);
|
|
315
307
|
if (run)
|
|
316
308
|
entry.run = run;
|
|
317
|
-
const setup =
|
|
309
|
+
const setup = asNonEmptyString(fmData.setup);
|
|
318
310
|
if (setup)
|
|
319
311
|
entry.setup = setup;
|
|
320
|
-
const cwd =
|
|
312
|
+
const cwd = asNonEmptyString(fmData.cwd);
|
|
321
313
|
if (cwd)
|
|
322
314
|
entry.cwd = cwd;
|
|
323
|
-
const quality =
|
|
315
|
+
const quality = asNonEmptyString(fmData.quality);
|
|
324
316
|
if (quality)
|
|
325
317
|
entry.quality = normalizeQuality(quality);
|
|
318
|
+
const beliefState = asNonEmptyString(fmData.beliefState);
|
|
319
|
+
if (beliefState)
|
|
320
|
+
entry.beliefState = beliefState;
|
|
321
|
+
const supersededBy = normalizeStringListOrUndefined(fmData.supersededBy);
|
|
322
|
+
if (supersededBy)
|
|
323
|
+
entry.supersededBy = supersededBy;
|
|
324
|
+
const contradictedBy = normalizeStringListOrUndefined(fmData.contradictedBy);
|
|
325
|
+
if (contradictedBy)
|
|
326
|
+
entry.contradictedBy = contradictedBy;
|
|
327
|
+
const currentBeliefRefs = normalizeStringListOrUndefined(fmData.currentBeliefRefs);
|
|
328
|
+
if (currentBeliefRefs)
|
|
329
|
+
entry.currentBeliefRefs = currentBeliefRefs;
|
|
326
330
|
const intent = normalizeIntent(fmData.intent);
|
|
327
331
|
if (intent)
|
|
328
332
|
entry.intent = intent;
|
|
@@ -416,6 +420,12 @@ export function shouldIndexStashFile(stashRoot, file, options) {
|
|
|
416
420
|
const segments = relPath.split(/[\\/]+/).filter(Boolean);
|
|
417
421
|
if (segments.length === 0)
|
|
418
422
|
return true;
|
|
423
|
+
// Skip vault .env files that have a sibling .sensitive marker file.
|
|
424
|
+
if (segments[0] === "vaults" && (file.endsWith(".env") || path.basename(file) === ".env")) {
|
|
425
|
+
const markerPath = file.replace(/\.env$/, ".sensitive");
|
|
426
|
+
if (fs.existsSync(markerPath))
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
419
429
|
if (options?.treatStashRootAsWikiRoot) {
|
|
420
430
|
return !(segments.length === 1 && WIKI_INFRA_FILES.has(segments[0]));
|
|
421
431
|
}
|
|
@@ -681,7 +691,137 @@ function mergeAliases(existing, generated) {
|
|
|
681
691
|
const merged = normalizeTerms([...(existing ?? []), ...generated]);
|
|
682
692
|
return merged.length > 0 ? merged : undefined;
|
|
683
693
|
}
|
|
694
|
+
// ── Enrichment Completeness ─────────────────────────────────────────────────
|
|
695
|
+
/**
|
|
696
|
+
* Returns `true` when a stash entry already has enough LLM-quality metadata
|
|
697
|
+
* that calling the LLM would produce no meaningful improvement.
|
|
698
|
+
*
|
|
699
|
+
* An entry is considered complete when ALL of the following hold:
|
|
700
|
+
* - `description` is a non-empty string
|
|
701
|
+
* - `tags` is a non-empty array
|
|
702
|
+
* - `searchHints` is a non-empty array
|
|
703
|
+
*
|
|
704
|
+
* This predicate is used by `enhanceDirsWithLlm` to skip the LLM call for
|
|
705
|
+
* entries that were previously enriched and already carry all three fields.
|
|
706
|
+
* Pass `reEnrich = true` in the caller to bypass this check.
|
|
707
|
+
*/
|
|
708
|
+
export function isEnrichmentComplete(entry) {
|
|
709
|
+
const hasDescription = typeof entry.description === "string" && entry.description.trim().length > 0;
|
|
710
|
+
const hasTags = Array.isArray(entry.tags) && entry.tags.length > 0;
|
|
711
|
+
const hasSearchHints = Array.isArray(entry.searchHints) && entry.searchHints.length > 0;
|
|
712
|
+
return hasDescription && hasTags && hasSearchHints;
|
|
713
|
+
}
|
|
684
714
|
// ── Metadata Generation ─────────────────────────────────────────────────────
|
|
715
|
+
/**
|
|
716
|
+
* Shared pipeline (steps 2-6) for building a single StashEntry from a file.
|
|
717
|
+
*
|
|
718
|
+
* Both `generateMetadata` and `generateMetadataFlat` perform identical work
|
|
719
|
+
* once the initial `entry` object has been seeded with type and canonical name.
|
|
720
|
+
* This helper encapsulates that shared pipeline so the two callers only differ
|
|
721
|
+
* in how they determine the asset type and canonical name (step 1):
|
|
722
|
+
*
|
|
723
|
+
* - `generateMetadata` — explicit `assetType` arg + `deriveCanonicalAssetName`
|
|
724
|
+
* - `generateMetadataFlat` — type from `runMatchers()` + `deriveCanonicalAssetNameFromStashRoot`
|
|
725
|
+
*
|
|
726
|
+
* @param file Absolute path to the file being processed.
|
|
727
|
+
* @param assetType Resolved asset type string (already validated by caller).
|
|
728
|
+
* @param canonicalName Resolved canonical name (already computed by caller).
|
|
729
|
+
* @param dirPath Directory containing the file (used for tag fallback).
|
|
730
|
+
* @param pkgMeta Pre-loaded package.json metadata for this directory (may be null/undefined).
|
|
731
|
+
* @param stashRoot Stash root used for renderer search hints context.
|
|
732
|
+
* @param ctx FileContext for the file (may be pre-built by the caller).
|
|
733
|
+
* @param match Pre-resolved MatchResult when available (from `generateMetadataFlat`).
|
|
734
|
+
* @returns The populated entry, or `{ skip: true, warning: string }` when the
|
|
735
|
+
* renderer throws and the file should be dropped.
|
|
736
|
+
*/
|
|
737
|
+
async function buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match) {
|
|
738
|
+
const ext = path.extname(file).toLowerCase();
|
|
739
|
+
const baseName = path.basename(file, ext);
|
|
740
|
+
const entry = {
|
|
741
|
+
name: canonicalName,
|
|
742
|
+
type: assetType,
|
|
743
|
+
quality: "generated",
|
|
744
|
+
confidence: 0.55,
|
|
745
|
+
source: "filename",
|
|
746
|
+
};
|
|
747
|
+
// Priority 1: Package.json metadata
|
|
748
|
+
if (pkgMeta) {
|
|
749
|
+
if (pkgMeta.description && !entry.description) {
|
|
750
|
+
entry.description = pkgMeta.description;
|
|
751
|
+
entry.source = "package";
|
|
752
|
+
entry.confidence = 0.8;
|
|
753
|
+
}
|
|
754
|
+
if (pkgMeta.keywords && pkgMeta.keywords.length > 0)
|
|
755
|
+
entry.tags = normalizeTerms(pkgMeta.keywords);
|
|
756
|
+
}
|
|
757
|
+
// Priority 2: Frontmatter (for .md files -- overrides package.json description)
|
|
758
|
+
if (ext === ".md") {
|
|
759
|
+
const content = ctx.content();
|
|
760
|
+
const parsed = parseFrontmatter(content);
|
|
761
|
+
applyCuratedFrontmatter(entry, parsed.data);
|
|
762
|
+
// Extract parameters from frontmatter params: key
|
|
763
|
+
const fmParams = extractFrontmatterParameters(parsed.data);
|
|
764
|
+
if (fmParams)
|
|
765
|
+
entry.parameters = fmParams;
|
|
766
|
+
// Pass wiki-pattern frontmatter through onto the entry
|
|
767
|
+
applyWikiFrontmatter(entry, parsed.data);
|
|
768
|
+
// Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
|
|
769
|
+
if (entry.type === "command") {
|
|
770
|
+
const cmdParams = extractCommandParameters(parsed.content);
|
|
771
|
+
if (cmdParams) {
|
|
772
|
+
entry.parameters = mergeParameters(entry.parameters, cmdParams);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
// Extract @param from script files.
|
|
777
|
+
// Vault files (.env) are deliberately excluded — their contents are secrets
|
|
778
|
+
// and must never be parsed for @param or any other metadata that could
|
|
779
|
+
// embed a value into the entry.
|
|
780
|
+
if (ext !== ".md" && assetType !== "vault") {
|
|
781
|
+
const content = ctx.content();
|
|
782
|
+
const scriptParams = extractScriptParameters(file, content);
|
|
783
|
+
if (scriptParams)
|
|
784
|
+
entry.parameters = scriptParams;
|
|
785
|
+
applyCommentMetadata(entry, extractCommentMetadata(file, content));
|
|
786
|
+
}
|
|
787
|
+
// Priority 3: Renderer metadata extraction
|
|
788
|
+
// When no pre-resolved match is available (generateMetadata path), run
|
|
789
|
+
// matchers now so the renderer can extract type-specific metadata.
|
|
790
|
+
const resolvedMatch = match ?? (await runMatchers(ctx));
|
|
791
|
+
if (resolvedMatch) {
|
|
792
|
+
const renderer = await getRenderer(resolvedMatch.renderer);
|
|
793
|
+
if (renderer) {
|
|
794
|
+
const renderCtx = buildRenderContext(ctx, resolvedMatch, [stashRoot]);
|
|
795
|
+
try {
|
|
796
|
+
await applyMetadataContributors(entry, {
|
|
797
|
+
rendererName: renderer.name,
|
|
798
|
+
renderContext: renderCtx,
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
catch (error) {
|
|
802
|
+
return {
|
|
803
|
+
skip: true,
|
|
804
|
+
warning: buildMetadataSkipWarning(file, assetType, error),
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
// Priority 4: Filename heuristics (fallback)
|
|
810
|
+
if (!entry.description) {
|
|
811
|
+
entry.description = fileNameToDescription(baseName);
|
|
812
|
+
entry.source = "filename";
|
|
813
|
+
entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
|
|
814
|
+
}
|
|
815
|
+
if (!entry.tags || entry.tags.length === 0) {
|
|
816
|
+
entry.tags = extractTagsFromPath(file, dirPath);
|
|
817
|
+
}
|
|
818
|
+
entry.tags = normalizeTerms(entry.tags ?? []);
|
|
819
|
+
entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
|
|
820
|
+
// Search hints are only generated when LLM is configured (via enhanceStashWithLlm)
|
|
821
|
+
// Heuristic search hints are too noisy to be useful for search quality
|
|
822
|
+
entry.filename = path.basename(file);
|
|
823
|
+
return entry;
|
|
824
|
+
}
|
|
685
825
|
export async function generateMetadata(dirPath, assetType, files, typeRoot = dirPath) {
|
|
686
826
|
const entries = [];
|
|
687
827
|
const warnings = [];
|
|
@@ -694,84 +834,16 @@ export async function generateMetadata(dirPath, assetType, files, typeRoot = dir
|
|
|
694
834
|
if (!isRelevantAssetFile(assetType, fileName))
|
|
695
835
|
continue;
|
|
696
836
|
const canonicalName = deriveCanonicalAssetName(assetType, typeRoot, file) ?? baseName;
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
type: assetType,
|
|
700
|
-
quality: "generated",
|
|
701
|
-
confidence: 0.55,
|
|
702
|
-
source: "filename",
|
|
703
|
-
};
|
|
704
|
-
// Priority 1: Package.json metadata
|
|
705
|
-
if (pkgMeta) {
|
|
706
|
-
if (pkgMeta.description && !entry.description) {
|
|
707
|
-
entry.description = pkgMeta.description;
|
|
708
|
-
entry.source = "package";
|
|
709
|
-
entry.confidence = 0.8;
|
|
710
|
-
}
|
|
711
|
-
if (pkgMeta.keywords && pkgMeta.keywords.length > 0)
|
|
712
|
-
entry.tags = normalizeTerms(pkgMeta.keywords);
|
|
713
|
-
}
|
|
714
|
-
// Priority 2: Frontmatter (for .md files -- overrides package.json description)
|
|
715
|
-
if (ext === ".md") {
|
|
716
|
-
const content = fs.readFileSync(file, "utf8");
|
|
717
|
-
const parsed = parseFrontmatter(content);
|
|
718
|
-
applyCuratedFrontmatter(entry, parsed.data);
|
|
719
|
-
// Extract parameters from frontmatter params: key
|
|
720
|
-
const fmParams = extractFrontmatterParameters(parsed.data);
|
|
721
|
-
if (fmParams)
|
|
722
|
-
entry.parameters = fmParams;
|
|
723
|
-
// Pass wiki-pattern frontmatter through onto the entry
|
|
724
|
-
applyWikiFrontmatter(entry, parsed.data);
|
|
725
|
-
// Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
|
|
726
|
-
if (entry.type === "command") {
|
|
727
|
-
const cmdParams = extractCommandParameters(parsed.content);
|
|
728
|
-
if (cmdParams) {
|
|
729
|
-
entry.parameters = mergeParameters(entry.parameters, cmdParams);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
// Extract @param from script files.
|
|
734
|
-
// Vault files (.env) are deliberately excluded — their contents are secrets
|
|
735
|
-
// and must never be parsed for @param or any other metadata that could
|
|
736
|
-
// embed a value into the entry.
|
|
737
|
-
if (ext !== ".md" && assetType !== "vault") {
|
|
738
|
-
const content = fs.readFileSync(file, "utf8");
|
|
739
|
-
const scriptParams = extractScriptParameters(file, content);
|
|
740
|
-
if (scriptParams)
|
|
741
|
-
entry.parameters = scriptParams;
|
|
742
|
-
applyCommentMetadata(entry, extractCommentMetadata(file, content));
|
|
743
|
-
}
|
|
744
|
-
// Priority 3: Type-specific metadata extraction (e.g. TOC for knowledge, comments for scripts)
|
|
837
|
+
// Build file context with typeRoot as the stash root so renderer context
|
|
838
|
+
// and search hints are scoped to the type directory.
|
|
745
839
|
const fileCtx = buildFileContext(typeRoot, file);
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
try {
|
|
752
|
-
renderer.extractMetadata(entry, renderCtx);
|
|
753
|
-
}
|
|
754
|
-
catch (error) {
|
|
755
|
-
warnings.push(buildMetadataSkipWarning(file, assetType, error));
|
|
756
|
-
continue;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
// Priority 4: Filename heuristics (fallback)
|
|
761
|
-
if (!entry.description) {
|
|
762
|
-
entry.description = fileNameToDescription(baseName);
|
|
763
|
-
entry.source = "filename";
|
|
764
|
-
entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
|
|
765
|
-
}
|
|
766
|
-
if (!entry.tags || entry.tags.length === 0) {
|
|
767
|
-
entry.tags = extractTagsFromPath(file, dirPath);
|
|
840
|
+
// Step 1: type is explicit; delegate steps 2-6 to the shared pipeline.
|
|
841
|
+
const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, typeRoot, fileCtx, null);
|
|
842
|
+
if ("skip" in result) {
|
|
843
|
+
warnings.push(result.warning);
|
|
844
|
+
continue;
|
|
768
845
|
}
|
|
769
|
-
|
|
770
|
-
entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
|
|
771
|
-
// Search hints are only generated when LLM is configured (via enhanceStashWithLlm)
|
|
772
|
-
// Heuristic search hints are too noisy to be useful for search quality
|
|
773
|
-
entry.filename = path.basename(file);
|
|
774
|
-
entries.push(entry);
|
|
846
|
+
entries.push(result);
|
|
775
847
|
}
|
|
776
848
|
return warnings.length > 0 ? { entries, warnings } : { entries };
|
|
777
849
|
}
|
|
@@ -789,6 +861,7 @@ export async function generateMetadataFlat(stashRoot, files) {
|
|
|
789
861
|
for (const file of files) {
|
|
790
862
|
if (!shouldIndexStashFile(stashRoot, file))
|
|
791
863
|
continue;
|
|
864
|
+
// Step 1: determine type and canonical name via the matcher system.
|
|
792
865
|
const ctx = buildFileContext(stashRoot, file);
|
|
793
866
|
const match = await runMatchers(ctx);
|
|
794
867
|
if (!match)
|
|
@@ -802,83 +875,20 @@ export async function generateMetadataFlat(stashRoot, files) {
|
|
|
802
875
|
const ext = path.extname(file).toLowerCase();
|
|
803
876
|
const baseName = path.basename(file, ext);
|
|
804
877
|
const canonicalName = deriveCanonicalAssetNameFromStashRoot(assetType, stashRoot, file) ?? baseName;
|
|
805
|
-
|
|
806
|
-
name: canonicalName,
|
|
807
|
-
type: assetType,
|
|
808
|
-
quality: "generated",
|
|
809
|
-
confidence: 0.55,
|
|
810
|
-
source: "filename",
|
|
811
|
-
};
|
|
812
|
-
// Package.json metadata
|
|
878
|
+
// Resolve package.json metadata with a per-directory cache.
|
|
813
879
|
const dirPath = path.dirname(file);
|
|
814
880
|
if (!pkgMetaCache.has(dirPath)) {
|
|
815
881
|
pkgMetaCache.set(dirPath, extractPackageMetadata(dirPath));
|
|
816
882
|
}
|
|
817
883
|
const pkgMeta = pkgMetaCache.get(dirPath);
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
if (pkgMeta.keywords?.length)
|
|
825
|
-
entry.tags = normalizeTerms(pkgMeta.keywords);
|
|
826
|
-
}
|
|
827
|
-
// Frontmatter
|
|
828
|
-
if (ext === ".md") {
|
|
829
|
-
const content = ctx.content();
|
|
830
|
-
const parsed = parseFrontmatter(content);
|
|
831
|
-
applyCuratedFrontmatter(entry, parsed.data);
|
|
832
|
-
// Extract parameters from frontmatter params: key
|
|
833
|
-
const fmParams = extractFrontmatterParameters(parsed.data);
|
|
834
|
-
if (fmParams)
|
|
835
|
-
entry.parameters = fmParams;
|
|
836
|
-
// Pass wiki-pattern frontmatter through onto the entry
|
|
837
|
-
applyWikiFrontmatter(entry, parsed.data);
|
|
838
|
-
// Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
|
|
839
|
-
if (entry.type === "command") {
|
|
840
|
-
const cmdParams = extractCommandParameters(parsed.content);
|
|
841
|
-
if (cmdParams) {
|
|
842
|
-
entry.parameters = mergeParameters(entry.parameters, cmdParams);
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
// Extract @param from script files.
|
|
847
|
-
// Vault files (.env) are deliberately excluded — their contents are secrets
|
|
848
|
-
// and must never be parsed for @param or any other metadata that could
|
|
849
|
-
// embed a value into the entry.
|
|
850
|
-
if (ext !== ".md" && assetType !== "vault") {
|
|
851
|
-
const content = ctx.content();
|
|
852
|
-
const scriptParams = extractScriptParameters(file, content);
|
|
853
|
-
if (scriptParams)
|
|
854
|
-
entry.parameters = scriptParams;
|
|
855
|
-
applyCommentMetadata(entry, extractCommentMetadata(file, content));
|
|
856
|
-
}
|
|
857
|
-
// Renderer metadata extraction
|
|
858
|
-
const renderer = await getRenderer(match.renderer);
|
|
859
|
-
if (renderer?.extractMetadata) {
|
|
860
|
-
const renderCtx = buildRenderContext(ctx, match, [stashRoot]);
|
|
861
|
-
try {
|
|
862
|
-
renderer.extractMetadata(entry, renderCtx);
|
|
863
|
-
}
|
|
864
|
-
catch (error) {
|
|
865
|
-
warnings.push(buildMetadataSkipWarning(file, assetType, error));
|
|
866
|
-
continue;
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
// Filename heuristics fallback
|
|
870
|
-
if (!entry.description) {
|
|
871
|
-
entry.description = fileNameToDescription(baseName);
|
|
872
|
-
entry.source = "filename";
|
|
873
|
-
entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
|
|
874
|
-
}
|
|
875
|
-
if (!entry.tags || entry.tags.length === 0) {
|
|
876
|
-
entry.tags = extractTagsFromPath(file, dirPath);
|
|
884
|
+
// Steps 2-6: delegate to the shared pipeline; pass the pre-resolved match
|
|
885
|
+
// so we don't run matchers a second time.
|
|
886
|
+
const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match);
|
|
887
|
+
if ("skip" in result) {
|
|
888
|
+
warnings.push(result.warning);
|
|
889
|
+
continue;
|
|
877
890
|
}
|
|
878
|
-
|
|
879
|
-
entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
|
|
880
|
-
entry.filename = path.basename(file);
|
|
881
|
-
entries.push(entry);
|
|
891
|
+
entries.push(result);
|
|
882
892
|
}
|
|
883
893
|
return warnings.length > 0 ? { entries, warnings } : { entries };
|
|
884
894
|
}
|
|
@@ -980,17 +990,6 @@ export function extractDescriptionFromComments(filePath) {
|
|
|
980
990
|
return hashLines.join(" ");
|
|
981
991
|
return null;
|
|
982
992
|
}
|
|
983
|
-
export function extractFrontmatterDescription(filePath) {
|
|
984
|
-
let content;
|
|
985
|
-
try {
|
|
986
|
-
content = fs.readFileSync(filePath, "utf8");
|
|
987
|
-
}
|
|
988
|
-
catch {
|
|
989
|
-
return null;
|
|
990
|
-
}
|
|
991
|
-
const parsed = parseFrontmatter(content);
|
|
992
|
-
return toStringOrUndefined(parsed.data.description) ?? null;
|
|
993
|
-
}
|
|
994
993
|
export function extractPackageMetadata(dirPath) {
|
|
995
994
|
const pkgPath = path.join(dirPath, "package.json");
|
|
996
995
|
if (!fs.existsSync(pkgPath))
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseAssetRef } from "../core/asset-ref";
|
|
4
|
+
import { resolveAssetPathFromName, TYPE_DIRS } from "../core/asset-spec";
|
|
5
|
+
import { isWithin } from "../core/common";
|
|
6
|
+
import { resolveSourcesForOrigin } from "../registry/origin-resolve";
|
|
7
|
+
import { lookup } from "./indexer";
|
|
8
|
+
import { resolveSourceEntries } from "./search-source";
|
|
9
|
+
function normalizeRef(ref) {
|
|
10
|
+
return typeof ref === "string" ? parseAssetRef(ref) : ref;
|
|
11
|
+
}
|
|
12
|
+
function buildDiskCandidates(sourcePath, ref, preserveDirectNameFallback) {
|
|
13
|
+
const typeDir = path.join(sourcePath, TYPE_DIRS[ref.type] ?? `${ref.type}s`);
|
|
14
|
+
const candidates = [
|
|
15
|
+
resolveAssetPathFromName(ref.type, typeDir, ref.name),
|
|
16
|
+
path.join(sourcePath, ref.type, `${ref.name}.md`),
|
|
17
|
+
path.join(sourcePath, ref.type, ref.name),
|
|
18
|
+
];
|
|
19
|
+
if (preserveDirectNameFallback) {
|
|
20
|
+
candidates.push(path.join(sourcePath, `${ref.name}.md`), path.join(sourcePath, ref.name));
|
|
21
|
+
}
|
|
22
|
+
return candidates;
|
|
23
|
+
}
|
|
24
|
+
function resolveDirectoryEntry(filePath, directoryIndexNames) {
|
|
25
|
+
let stat;
|
|
26
|
+
try {
|
|
27
|
+
stat = fs.statSync(filePath);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (stat.isFile())
|
|
33
|
+
return filePath;
|
|
34
|
+
if (!stat.isDirectory())
|
|
35
|
+
return null;
|
|
36
|
+
for (const indexName of directoryIndexNames) {
|
|
37
|
+
const candidate = path.join(filePath, indexName);
|
|
38
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile())
|
|
39
|
+
return candidate;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
async function resolveViaIndex(ref) {
|
|
44
|
+
try {
|
|
45
|
+
const entry = await lookup(ref);
|
|
46
|
+
return entry?.filePath ?? null;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function resolveViaDisk(ref, options) {
|
|
53
|
+
let sources = resolveSourceEntries(options.stashDir);
|
|
54
|
+
if (options.honorOrigin !== false) {
|
|
55
|
+
sources = resolveSourcesForOrigin(ref.origin, sources);
|
|
56
|
+
}
|
|
57
|
+
const directoryIndexNames = options.directoryIndexNames ?? ["SKILL.md"];
|
|
58
|
+
const preserveDirectNameFallback = options.preserveDirectNameFallback ?? true;
|
|
59
|
+
for (const source of sources) {
|
|
60
|
+
if (options.writableDirSet && !options.writableDirSet.has(path.resolve(source.path)))
|
|
61
|
+
continue;
|
|
62
|
+
const candidates = buildDiskCandidates(source.path, ref, preserveDirectNameFallback);
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
if (!fs.existsSync(candidate))
|
|
65
|
+
continue;
|
|
66
|
+
const resolved = resolveDirectoryEntry(candidate, directoryIndexNames);
|
|
67
|
+
if (!resolved)
|
|
68
|
+
continue;
|
|
69
|
+
const resolvedRoot = fs.realpathSync(source.path);
|
|
70
|
+
const realTarget = fs.realpathSync(resolved);
|
|
71
|
+
if (!isWithin(realTarget, resolvedRoot))
|
|
72
|
+
continue;
|
|
73
|
+
return realTarget;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
export async function resolveAssetPath(ref, options = {}) {
|
|
79
|
+
const parsed = normalizeRef(ref);
|
|
80
|
+
const mode = options.mode ?? "index-first";
|
|
81
|
+
if (mode !== "disk-only") {
|
|
82
|
+
const indexed = await resolveViaIndex(parsed);
|
|
83
|
+
if (indexed)
|
|
84
|
+
return indexed;
|
|
85
|
+
if (mode === "index-only")
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return resolveViaDisk(parsed, options);
|
|
89
|
+
}
|