skilld 0.15.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/_chunks/{detect-imports.mjs → agent.mjs} +48 -15
- package/dist/_chunks/agent.mjs.map +1 -0
- package/dist/_chunks/{storage.mjs → cache.mjs} +81 -1
- package/dist/_chunks/cache.mjs.map +1 -0
- package/dist/_chunks/cache2.mjs +71 -0
- package/dist/_chunks/cache2.mjs.map +1 -0
- package/dist/_chunks/config.mjs +23 -0
- package/dist/_chunks/config.mjs.map +1 -1
- package/dist/_chunks/{embedding-cache2.mjs → embedding-cache.mjs} +1 -1
- package/dist/_chunks/embedding-cache.mjs.map +1 -0
- package/dist/_chunks/formatting.mjs +634 -0
- package/dist/_chunks/formatting.mjs.map +1 -0
- package/dist/_chunks/{version.d.mts → index.d.mts} +1 -1
- package/dist/_chunks/index.d.mts.map +1 -0
- package/dist/_chunks/{utils.d.mts → index2.d.mts} +1 -1
- package/dist/_chunks/index2.d.mts.map +1 -0
- package/dist/_chunks/install.mjs +539 -0
- package/dist/_chunks/install.mjs.map +1 -0
- package/dist/_chunks/list.mjs +70 -0
- package/dist/_chunks/list.mjs.map +1 -0
- package/dist/_chunks/markdown.mjs +7 -0
- package/dist/_chunks/markdown.mjs.map +1 -1
- package/dist/_chunks/pool.mjs +174 -0
- package/dist/_chunks/pool.mjs.map +1 -0
- package/dist/_chunks/pool2.mjs +1 -6
- package/dist/_chunks/pool2.mjs.map +1 -1
- package/dist/_chunks/prompts.mjs +234 -2
- package/dist/_chunks/prompts.mjs.map +1 -1
- package/dist/_chunks/sanitize.mjs +71 -0
- package/dist/_chunks/sanitize.mjs.map +1 -1
- package/dist/_chunks/search-interactive.mjs +245 -0
- package/dist/_chunks/search-interactive.mjs.map +1 -0
- package/dist/_chunks/search.mjs +12 -0
- package/dist/_chunks/shared.mjs +4 -0
- package/dist/_chunks/shared.mjs.map +1 -1
- package/dist/_chunks/{npm.mjs → sources.mjs} +401 -4
- package/dist/_chunks/sources.mjs.map +1 -0
- package/dist/_chunks/sync.mjs +1937 -0
- package/dist/_chunks/sync.mjs.map +1 -0
- package/dist/_chunks/sync2.mjs +13 -0
- package/dist/_chunks/uninstall.mjs +207 -0
- package/dist/_chunks/uninstall.mjs.map +1 -0
- package/dist/_chunks/validate.mjs +3 -0
- package/dist/_chunks/validate.mjs.map +1 -1
- package/dist/_chunks/yaml.mjs +19 -0
- package/dist/_chunks/yaml.mjs.map +1 -1
- package/dist/agent/index.d.mts +1 -1
- package/dist/agent/index.mjs +4 -3
- package/dist/cache/index.d.mts +2 -2
- package/dist/cache/index.mjs +2 -1
- package/dist/cli.mjs +146 -3823
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +2 -3
- package/dist/index.mjs +4 -4
- package/dist/retriv/index.mjs +14 -2
- package/dist/retriv/index.mjs.map +1 -1
- package/dist/retriv/worker.mjs +3 -3
- package/dist/sources/index.d.mts +2 -2
- package/dist/sources/index.mjs +2 -1
- package/dist/types.d.mts +2 -3
- package/package.json +9 -9
- package/dist/_chunks/detect-imports.mjs.map +0 -1
- package/dist/_chunks/embedding-cache2.mjs.map +0 -1
- package/dist/_chunks/npm.mjs.map +0 -1
- package/dist/_chunks/storage.mjs.map +0 -1
- package/dist/_chunks/utils.d.mts.map +0 -1
- package/dist/_chunks/version.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -8,12 +8,14 @@
|
|
|
8
8
|
|
|
9
9
|
## Why?
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
When using new packages or migrating to new versions, agents often struggle to use the appropriate best practices. This is because
|
|
12
|
+
agents have [knowledge cutoffs](https://platform.claude.com/docs/en/about-claude/models/overview#latest-models-comparison) and
|
|
13
|
+
predict based on existing patterns.
|
|
12
14
|
|
|
13
|
-
Methods of getting the right context to your agent require either manual curation, author opt-in, or
|
|
15
|
+
Methods of getting the right context to your agent require either manual curation, author opt-in, external servers or vendor lock-in. See [the landscape](#the-landscape)
|
|
14
16
|
for more details.
|
|
15
17
|
|
|
16
|
-
Skilld generates [agent skills](https://agentskills.io/home) from the
|
|
18
|
+
Skilld generates [agent skills](https://agentskills.io/home) from the references maintainers already create: docs, release notes and GitHub issues. With these we can create version-aware, local-first, and optimized skills.
|
|
17
19
|
|
|
18
20
|
<p align="center">
|
|
19
21
|
<table>
|
|
@@ -28,10 +30,10 @@ Skilld generates [agent skills](https://agentskills.io/home) from the docs maint
|
|
|
28
30
|
## Features
|
|
29
31
|
|
|
30
32
|
- 🌍 **Any Source: Opt-in** - Any NPM dependency or GitHub source, docs auto-resolved
|
|
31
|
-
- 📦 **Bleeding Edge Context** - Latest issues, discussions, and releases
|
|
32
|
-
every update. Always use the latest best practices and avoid deprecated patterns.
|
|
33
|
+
- 📦 **Bleeding Edge Context** - Latest issues, discussions, and releases . Always use the latest best practices and avoid deprecated patterns.
|
|
33
34
|
- 📚 **Opt-in LLM Sections** - Enhance skills with LLM-generated `Best Practices`, `API Changes`, or your own custom prompts
|
|
34
35
|
- 🔍 **Semantic Search** - Query indexed docs across all skills via [retriv](https://github.com/harlan-zw/retriv) embeddings
|
|
36
|
+
- 🧠 **Context-Aware** - Follows [Claude Code skill best practices](https://code.claude.com/docs/en/skills#add-supporting-files): SKILL.md stays under 500 lines, references are separate files the agent discovers on-demand — not inlined into context
|
|
35
37
|
- 🎯 **Safe & Versioned** - Prompt injection sanitization, version-aware caching, auto-updates on new releases
|
|
36
38
|
- 🤝 **Ecosystem** - Compatible with [`npx skills`](https://skills.sh/) and [skills-npm](https://github.com/antfu/skills-npm)
|
|
37
39
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as __exportAll } from "./chunk.mjs";
|
|
2
2
|
import { n as sanitizeMarkdown } from "./sanitize.mjs";
|
|
3
|
-
import { g as readCachedSection, v as writeSections } from "./
|
|
3
|
+
import { g as readCachedSection, v as writeSections } from "./cache.mjs";
|
|
4
4
|
import { i as resolveSkilldCommand } from "./shared.mjs";
|
|
5
5
|
import { _ as targets, c as SECTION_OUTPUT_FILES, d as getSectionValidator, l as buildAllSectionPrompts, m as detectInstalledAgents, s as SECTION_MERGE_ORDER } from "./prompts.mjs";
|
|
6
6
|
import { homedir } from "node:os";
|
|
@@ -8,7 +8,7 @@ import { dirname, join } from "pathe";
|
|
|
8
8
|
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, unlinkSync, writeFileSync } from "node:fs";
|
|
9
9
|
import { exec, spawn } from "node:child_process";
|
|
10
10
|
import { isWindows } from "std-env";
|
|
11
|
-
import {
|
|
11
|
+
import { glob } from "tinyglobby";
|
|
12
12
|
import { findDynamicImports, findStaticImports } from "mlly";
|
|
13
13
|
import { createHash } from "node:crypto";
|
|
14
14
|
import { setTimeout } from "node:timers/promises";
|
|
@@ -43,7 +43,6 @@ const models$2 = {
|
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
45
|
function buildArgs$2(model, skillDir, symlinkDirs) {
|
|
46
|
-
const skilldDir = join(skillDir, ".skilld");
|
|
47
46
|
return [
|
|
48
47
|
"-p",
|
|
49
48
|
"--model",
|
|
@@ -54,14 +53,11 @@ function buildArgs$2(model, skillDir, symlinkDirs) {
|
|
|
54
53
|
"--include-partial-messages",
|
|
55
54
|
"--allowedTools",
|
|
56
55
|
[
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
`Write(${skilldDir}/**)`,
|
|
63
|
-
`Bash(*skilld search*)`,
|
|
64
|
-
`Bash(*skilld validate*)`
|
|
56
|
+
"Read",
|
|
57
|
+
"Glob",
|
|
58
|
+
"Grep",
|
|
59
|
+
"Bash(*skilld search*)",
|
|
60
|
+
"Bash(*skilld validate*)"
|
|
65
61
|
].join(" "),
|
|
66
62
|
"--disallowedTools",
|
|
67
63
|
"WebSearch WebFetch Task",
|
|
@@ -71,6 +67,16 @@ function buildArgs$2(model, skillDir, symlinkDirs) {
|
|
|
71
67
|
"--no-session-persistence"
|
|
72
68
|
];
|
|
73
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Parse claude stream-json events
|
|
72
|
+
*
|
|
73
|
+
* Event types:
|
|
74
|
+
* - stream_event/content_block_delta/text_delta → token streaming
|
|
75
|
+
* - stream_event/content_block_start/tool_use → tool invocation starting
|
|
76
|
+
* - assistant message with tool_use content → tool name + input
|
|
77
|
+
* - assistant message with text content → full text (non-streaming fallback)
|
|
78
|
+
* - result → usage, cost, turns
|
|
79
|
+
*/
|
|
74
80
|
function parseLine$2(line) {
|
|
75
81
|
try {
|
|
76
82
|
const obj = JSON.parse(line);
|
|
@@ -222,6 +228,7 @@ function buildArgs(model, skillDir, symlinkDirs) {
|
|
|
222
228
|
...symlinkDirs.flatMap((d) => ["--include-directories", d])
|
|
223
229
|
];
|
|
224
230
|
}
|
|
231
|
+
/** Parse gemini stream-json events — turn level (full message per event) */
|
|
225
232
|
function parseLine(line) {
|
|
226
233
|
try {
|
|
227
234
|
const obj = JSON.parse(line);
|
|
@@ -267,6 +274,7 @@ const TOOL_VERBS = {
|
|
|
267
274
|
search_file_content: "Searching",
|
|
268
275
|
run_shell_command: "Running"
|
|
269
276
|
};
|
|
277
|
+
/** Create a progress callback that emits one line per tool call, Claude Code style */
|
|
270
278
|
function createToolProgress(log) {
|
|
271
279
|
let lastMsg = "";
|
|
272
280
|
let repeatCount = 0;
|
|
@@ -358,6 +366,7 @@ async function getAvailableModels() {
|
|
|
358
366
|
agentName: targets[config.agentId]?.displayName ?? config.agentId
|
|
359
367
|
}));
|
|
360
368
|
}
|
|
369
|
+
/** Resolve symlinks in .skilld/ to get real paths for --add-dir */
|
|
361
370
|
function resolveReferenceDirs(skillDir) {
|
|
362
371
|
const refsDir = join(skillDir, ".skilld");
|
|
363
372
|
if (!existsSync(refsDir)) return [];
|
|
@@ -370,6 +379,7 @@ function resolveReferenceDirs(skillDir) {
|
|
|
370
379
|
return [...resolved, ...parents];
|
|
371
380
|
}
|
|
372
381
|
const CACHE_DIR = join(homedir(), ".skilld", "llm-cache");
|
|
382
|
+
/** Strip absolute paths from prompt so the hash is project-independent */
|
|
373
383
|
function normalizePromptForHash(prompt) {
|
|
374
384
|
return prompt.replace(/\/[^\s`]*\.(?:claude|codex|gemini)\/skills\/[^\s/`]+/g, "<SKILL_DIR>");
|
|
375
385
|
}
|
|
@@ -398,6 +408,7 @@ function setCache(prompt, model, section, text) {
|
|
|
398
408
|
timestamp: Date.now()
|
|
399
409
|
}), { mode: 384 });
|
|
400
410
|
}
|
|
411
|
+
/** Spawn a single CLI process for one section */
|
|
401
412
|
function optimizeSection(opts) {
|
|
402
413
|
const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug, preExistingFiles } = opts;
|
|
403
414
|
const cliConfig = CLI_MODELS[model];
|
|
@@ -738,18 +749,21 @@ async function optimizeDocs(opts) {
|
|
|
738
749
|
debugLogsDir
|
|
739
750
|
};
|
|
740
751
|
}
|
|
752
|
+
/** Shorten absolute paths for display: /home/user/project/.claude/skills/vue/SKILL.md → .claude/.../SKILL.md */
|
|
741
753
|
function shortenPath(p) {
|
|
742
754
|
const refIdx = p.indexOf(".skilld/");
|
|
743
755
|
if (refIdx !== -1) return p.slice(refIdx + 8);
|
|
744
756
|
const parts = p.split("/");
|
|
745
757
|
return parts.length > 2 ? `.../${parts.slice(-2).join("/")}` : p;
|
|
746
758
|
}
|
|
759
|
+
/** Replace absolute paths in a command string with shortened versions */
|
|
747
760
|
function shortenCommand(cmd) {
|
|
748
761
|
return cmd.replace(/\/[^\s"']+/g, (match) => {
|
|
749
762
|
if (match.includes(".claude/") || match.includes(".skilld/") || match.includes("node_modules/")) return `.../${match.split("/").slice(-2).join("/")}`;
|
|
750
763
|
return match;
|
|
751
764
|
});
|
|
752
765
|
}
|
|
766
|
+
/** Clean a single section's LLM output: strip markdown fences, frontmatter, sanitize */
|
|
753
767
|
function cleanSectionOutput(content) {
|
|
754
768
|
let cleaned = content.trim();
|
|
755
769
|
const wrapMatch = cleaned.match(/^```(?:markdown|md)?[^\S\n]*\n([\s\S]+)\n```[^\S\n]*$/);
|
|
@@ -757,6 +771,7 @@ function cleanSectionOutput(content) {
|
|
|
757
771
|
const inner = wrapMatch[1].trim();
|
|
758
772
|
if (/^```(?:markdown|md)/.test(cleaned) || /^##\s/m.test(inner) || /^- (?:BREAKING|DEPRECATED|NEW): /m.test(inner)) cleaned = inner;
|
|
759
773
|
}
|
|
774
|
+
cleaned = cleaned.replace(/^# (?!#)/gm, "## ");
|
|
760
775
|
const fmMatch = cleaned.match(/^-{3,}\n/);
|
|
761
776
|
if (fmMatch) {
|
|
762
777
|
const afterOpen = fmMatch[0].length;
|
|
@@ -804,6 +819,10 @@ async function findNuxtConfig(cwd) {
|
|
|
804
819
|
}
|
|
805
820
|
return null;
|
|
806
821
|
}
|
|
822
|
+
/**
|
|
823
|
+
* Walk AST node to find all string values inside a `modules` array property.
|
|
824
|
+
* Handles: defineNuxtConfig({ modules: [...] }) and export default { modules: [...] }
|
|
825
|
+
*/
|
|
807
826
|
function extractModuleStrings(node) {
|
|
808
827
|
if (!node || typeof node !== "object") return [];
|
|
809
828
|
if (node.type === "Property" && !node.computed && node.key?.type === "Identifier" && node.key.name === "modules" && node.value?.type === "ArrayExpression") return node.value.elements.filter((el) => el?.type === "Literal" && typeof el.value === "string").map((el) => el.value);
|
|
@@ -816,6 +835,9 @@ function extractModuleStrings(node) {
|
|
|
816
835
|
}
|
|
817
836
|
return results;
|
|
818
837
|
}
|
|
838
|
+
/**
|
|
839
|
+
* Detect Nuxt modules from nuxt.config.{ts,js,mjs}
|
|
840
|
+
*/
|
|
819
841
|
async function detectNuxtModules(cwd) {
|
|
820
842
|
const config = await findNuxtConfig(cwd);
|
|
821
843
|
if (!config) return [];
|
|
@@ -840,9 +862,16 @@ async function detectNuxtModules(cwd) {
|
|
|
840
862
|
}
|
|
841
863
|
return packages;
|
|
842
864
|
}
|
|
865
|
+
/**
|
|
866
|
+
* Run all preset detectors and merge results
|
|
867
|
+
*/
|
|
843
868
|
async function detectPresetPackages(cwd) {
|
|
844
869
|
return detectNuxtModules(cwd);
|
|
845
870
|
}
|
|
871
|
+
/**
|
|
872
|
+
* Detect directly-used npm packages by scanning source files
|
|
873
|
+
* Uses mlly for proper ES module parsing + tinyglobby for file discovery
|
|
874
|
+
*/
|
|
846
875
|
const PATTERNS = ["**/*.{ts,js,vue,mjs,cjs,tsx,jsx,mts,cts}"];
|
|
847
876
|
const IGNORE = [
|
|
848
877
|
"**/node_modules/**",
|
|
@@ -856,14 +885,18 @@ function addPackage(counts, specifier) {
|
|
|
856
885
|
const name = specifier.startsWith("@") ? specifier.split("/").slice(0, 2).join("/") : specifier.split("/")[0];
|
|
857
886
|
if (!isNodeBuiltin(name)) counts.set(name, (counts.get(name) || 0) + 1);
|
|
858
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* Scan source files to detect all directly-imported npm packages
|
|
890
|
+
* Async with gitignore support for proper spinner animation
|
|
891
|
+
*/
|
|
859
892
|
async function detectImportedPackages(cwd = process.cwd()) {
|
|
860
893
|
try {
|
|
861
894
|
const counts = /* @__PURE__ */ new Map();
|
|
862
|
-
const files = await
|
|
895
|
+
const files = await glob(PATTERNS, {
|
|
863
896
|
cwd,
|
|
864
897
|
ignore: IGNORE,
|
|
865
|
-
|
|
866
|
-
|
|
898
|
+
absolute: true,
|
|
899
|
+
expandDirectories: false
|
|
867
900
|
});
|
|
868
901
|
await Promise.all(files.map(async (file) => {
|
|
869
902
|
const content = await readFile(file, "utf8");
|
|
@@ -934,4 +967,4 @@ function isNodeBuiltin(pkg) {
|
|
|
934
967
|
}
|
|
935
968
|
export { getModelName as a, getModelLabel as i, createToolProgress as n, optimizeDocs as o, getAvailableModels as r, detectImportedPackages as t };
|
|
936
969
|
|
|
937
|
-
//# sourceMappingURL=
|
|
970
|
+
//# sourceMappingURL=agent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.mjs","names":["cli","agentId","models","buildArgs","parseLine","cli","agentId","models","buildArgs","parseLine","claude","gemini","codex","claude.buildArgs","gemini.buildArgs","codex.buildArgs","claude.parseLine","gemini.parseLine","codex.parseLine","agents","delay"],"sources":["../../src/agent/clis/claude.ts","../../src/agent/clis/codex.ts","../../src/agent/clis/gemini.ts","../../src/agent/clis/index.ts","../../src/agent/detect-presets.ts","../../src/agent/detect-imports.ts"],"sourcesContent":["/**\n * Claude Code CLI — token-level streaming via --include-partial-messages\n *\n * Write permission: Claude Code has hardcoded .claude/ write protection and\n * --allowedTools glob patterns are broken (github.com/anthropics/claude-code/issues/6881).\n * Instead of fighting the permission system, we let Write be auto-denied in pipe mode\n * and capture the content via writeContent fallback in parseLine().\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'claude' as const\nexport const agentId = 'claude-code' as const\n\nexport const models: Record<string, CliModelEntry> = {\n opus: { model: 'opus', name: 'Opus 4.6', hint: 'Most capable for complex work' },\n sonnet: { model: 'sonnet', name: 'Sonnet 4.6', hint: 'Best for everyday tasks' },\n haiku: { model: 'haiku', name: 'Haiku 4.5', hint: 'Fastest for quick answers', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n const allowedTools = [\n // Bare tool names — --add-dir already scopes visibility\n 'Read',\n 'Glob',\n 'Grep',\n 'Bash(*skilld search*)',\n 'Bash(*skilld validate*)',\n // Write intentionally omitted — auto-denied in pipe mode, content\n // captured via writeContent fallback (see parseLine + index.ts:373)\n ].join(' ')\n return [\n '-p',\n '--model',\n model,\n '--output-format',\n 'stream-json',\n '--verbose',\n '--include-partial-messages',\n '--allowedTools',\n allowedTools,\n '--disallowedTools',\n 'WebSearch WebFetch Task',\n '--add-dir',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--add-dir', d]),\n '--no-session-persistence',\n ]\n}\n\n/**\n * Parse claude stream-json events\n *\n * Event types:\n * - stream_event/content_block_delta/text_delta → token streaming\n * - stream_event/content_block_start/tool_use → tool invocation starting\n * - assistant message with tool_use content → tool name + input\n * - assistant message with text content → full text (non-streaming fallback)\n * - result → usage, cost, turns\n */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Token-level streaming (--include-partial-messages)\n if (obj.type === 'stream_event') {\n const evt = obj.event\n if (!evt)\n return {}\n\n // Text delta — the main streaming path\n if (evt.type === 'content_block_delta' && evt.delta?.type === 'text_delta') {\n return { textDelta: evt.delta.text }\n }\n\n return {}\n }\n\n // Full assistant message (complete turn, after streaming)\n if (obj.type === 'assistant' && obj.message?.content) {\n const content = obj.message.content as any[]\n\n // Extract tool uses with inputs for progress hints\n const tools = content.filter((c: any) => c.type === 'tool_use')\n if (tools.length) {\n const names = tools.map((t: any) => t.name)\n // Extract useful hint from tool input (file path, query, etc)\n const hint = tools.map((t: any) => {\n const input = t.input || {}\n return input.file_path || input.path || input.pattern || input.query || input.command || ''\n }).filter(Boolean).join(', ')\n // Capture Write content — primary output path since Write is auto-denied\n const writeTool = tools.find((t: any) => t.name === 'Write' && t.input?.content)\n return { toolName: names.join(', '), toolHint: hint || undefined, writeContent: writeTool?.input?.content }\n }\n\n // Text content (fallback for non-partial mode)\n const text = content\n .filter((c: any) => c.type === 'text')\n .map((c: any) => c.text)\n .join('')\n if (text)\n return { fullText: text }\n }\n\n // Final result\n if (obj.type === 'result') {\n const u = obj.usage\n return {\n done: true,\n usage: u ? { input: u.input_tokens ?? u.inputTokens ?? 0, output: u.output_tokens ?? u.outputTokens ?? 0 } : undefined,\n cost: obj.total_cost_usd,\n turns: obj.num_turns,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * OpenAI Codex CLI — exec subcommand with JSON output\n * Prompt passed via stdin with `-` sentinel\n *\n * Event types:\n * - turn.started / turn.completed → turn lifecycle + usage\n * - item.started → command_execution in progress\n * - item.completed → agent_message (text), command_execution (result), file_change (apply_patch)\n * - error / turn.failed → errors\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'codex' as const\nexport const agentId = 'codex' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gpt-5.3-codex': { model: 'gpt-5.3-codex', name: 'GPT-5.3 Codex', hint: 'Latest frontier Codex model' },\n 'gpt-5.3-codex-spark': { model: 'gpt-5.3-codex-spark', name: 'GPT-5.3 Codex Spark', hint: 'Faster GPT-5.3 Codex variant', recommended: true },\n 'gpt-5.2-codex': { model: 'gpt-5.2-codex', name: 'GPT-5.2 Codex', hint: 'Frontier agentic coding model' },\n}\n\nexport function buildArgs(model: string, _skillDir: string, _symlinkDirs: string[]): string[] {\n return [\n 'exec',\n '--json',\n '--ephemeral',\n '--model',\n model,\n // Permissions aligned with Claude's scoped model:\n // --full-auto = --sandbox workspace-write + --ask-for-approval on-request\n // → writes scoped to CWD (.skilld/, set in spawn), reads unrestricted, network blocked\n // Shell remains enabled for `skilld` / `npx -y skilld` search/validate (no per-command allowlist in Codex)\n // --ephemeral → no session persistence (equivalent to Claude's --no-session-persistence)\n '--full-auto',\n '-',\n ]\n}\n\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'item.completed' && obj.item) {\n const item = obj.item\n // Agent message — the main text output\n if (item.type === 'agent_message' && item.text)\n return { fullText: item.text }\n // Command execution completed — log as tool progress\n // If the command writes to a file (redirect or cat >), capture output as writeContent fallback\n if (item.type === 'command_execution' && item.aggregated_output) {\n const cmd = item.command || ''\n const writeContent = (/^cat\\s*>|>/.test(cmd)) ? item.aggregated_output : undefined\n return { toolName: 'Bash', toolHint: `(${item.aggregated_output.length} chars output)`, writeContent }\n }\n // apply_patch completed — file written directly to disk\n if (item.type === 'file_change' && item.changes?.length) {\n const paths = item.changes.map((c: { path: string, kind: string }) => c.path).join(', ')\n return { toolName: 'Write', toolHint: paths }\n }\n }\n\n // Command starting — show progress\n if (obj.type === 'item.started' && obj.item?.type === 'command_execution') {\n return { toolName: 'Bash', toolHint: obj.item.command }\n }\n\n // Turn completed — usage stats\n if (obj.type === 'turn.completed' && obj.usage) {\n return {\n done: true,\n usage: {\n input: obj.usage.input_tokens ?? 0,\n output: obj.usage.output_tokens ?? 0,\n },\n }\n }\n\n // Error events\n if (obj.type === 'turn.failed' || obj.type === 'error') {\n return { done: true }\n }\n }\n catch {}\n return {}\n}\n","/**\n * Gemini CLI — turn-level streaming via -o stream-json\n * Write scoping: relies on cwd being set to .skilld/ (no native --writeable-dirs)\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\nimport { resolveSkilldCommand } from '../../core/shared.ts'\n\nexport const cli = 'gemini' as const\nexport const agentId = 'gemini-cli' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gemini-3-pro': { model: 'gemini-3-pro-preview', name: 'Gemini 3 Pro', hint: 'Most capable' },\n 'gemini-3-flash': { model: 'gemini-3-flash-preview', name: 'Gemini 3 Flash', hint: 'Balanced', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n return [\n '-o',\n 'stream-json',\n '-m',\n model,\n '--allowed-tools',\n `read_file,write_file,glob_tool,list_directory,search_file_content,run_shell_command(${resolveSkilldCommand()}),run_shell_command(grep),run_shell_command(head)`,\n '--include-directories',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--include-directories', d]),\n ]\n}\n\n/** Parse gemini stream-json events — turn level (full message per event) */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Text message (delta or full)\n if (obj.type === 'message' && obj.role === 'assistant' && obj.content) {\n return obj.delta ? { textDelta: obj.content } : { fullText: obj.content }\n }\n\n // Tool invocation\n if (obj.type === 'tool_use' || obj.type === 'tool_call') {\n const name = obj.tool_name || obj.name || obj.tool || 'tool'\n const params = obj.parameters || obj.args || obj.input || {}\n const hint = params.file_path || params.path || params.dir_path || params.pattern || params.query || params.command || ''\n // Capture write_file content as fallback (matches Claude's Write tool behavior)\n if (name === 'write_file' && params.content) {\n return { toolName: name, toolHint: hint || undefined, writeContent: params.content }\n }\n return { toolName: name, toolHint: hint || undefined }\n }\n\n // Final result\n if (obj.type === 'result') {\n const s = obj.stats\n return {\n done: true,\n usage: s ? { input: s.input_tokens ?? s.input ?? 0, output: s.output_tokens ?? s.output ?? 0 } : undefined,\n turns: s?.tool_calls,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * CLI orchestrator — spawns per-CLI processes for skill generation\n * Each CLI (claude, gemini, codex) has its own buildArgs + parseLine in separate files\n */\n\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { AgentType } from '../types.ts'\nimport type { CliModelConfig, CliName, OptimizeDocsOptions, OptimizeModel, OptimizeResult, ParsedEvent, SectionResult, StreamProgress, ValidationWarning } from './types.ts'\nimport { exec, spawn } from 'node:child_process'\nimport { createHash } from 'node:crypto'\nimport { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, unlinkSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { setTimeout as delay } from 'node:timers/promises'\nimport { promisify } from 'node:util'\nimport { dirname, join } from 'pathe'\nimport { isWindows } from 'std-env'\nimport { readCachedSection, writeSections } from '../../cache/index.ts'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\nimport { detectInstalledAgents } from '../detect.ts'\nimport { buildAllSectionPrompts, getSectionValidator, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES } from '../prompts/index.ts'\nimport { agents } from '../registry.ts'\nimport * as claude from './claude.ts'\nimport * as codex from './codex.ts'\nimport * as gemini from './gemini.ts'\n\nexport { buildAllSectionPrompts, buildSectionPrompt, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES } from '../prompts/index.ts'\nexport type { CustomPrompt, SkillSection } from '../prompts/index.ts'\nexport type { CliModelConfig, CliName, ModelInfo, OptimizeDocsOptions, OptimizeModel, OptimizeResult, StreamProgress } from './types.ts'\n\n// ── Tool progress display ────────────────────────────────────────────\n\nconst TOOL_VERBS: Record<string, string> = {\n // Claude\n Read: 'Reading',\n Glob: 'Searching',\n Grep: 'Searching',\n Write: 'Writing',\n Bash: 'Running',\n // Gemini\n read_file: 'Reading',\n glob_tool: 'Searching',\n write_file: 'Writing',\n list_directory: 'Listing',\n search_file_content: 'Searching',\n run_shell_command: 'Running',\n}\n\ninterface ToolProgressLog {\n message: (msg: string) => void\n}\n\n/** Create a progress callback that emits one line per tool call, Claude Code style */\nexport function createToolProgress(log: ToolProgressLog): (progress: StreamProgress) => void {\n let lastMsg = ''\n let repeatCount = 0\n\n function emit(msg: string) {\n if (msg === lastMsg) {\n repeatCount++\n log.message(`${msg} \\x1B[90m(+${repeatCount})\\x1B[0m`)\n }\n else {\n lastMsg = msg\n repeatCount = 0\n log.message(msg)\n }\n }\n\n return ({ type, chunk, section }) => {\n if (type === 'text') {\n emit(`${section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''}Writing...`)\n return\n }\n if (type !== 'reasoning' || !chunk.startsWith('['))\n return\n\n // Parse individual tool names and hints from \"[Read: path]\" or \"[Read, Glob: path1, path2]\"\n const match = chunk.match(/^\\[([^:[\\]]+)(?::\\s(.+))?\\]$/)\n if (!match)\n return\n\n const names = match[1]!.split(',').map(n => n.trim())\n const hints = match[2]?.split(',').map(h => h.trim()) ?? []\n\n for (let i = 0; i < names.length; i++) {\n const rawName = names[i]!\n const hint = hints[i] ?? hints[0] ?? ''\n const verb = TOOL_VERBS[rawName] ?? rawName\n const prefix = section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''\n\n if ((rawName === 'Bash' || rawName === 'run_shell_command') && hint) {\n const searchMatch = hint.match(/skilld search\\s+\"([^\"]+)\"/)\n if (searchMatch) {\n emit(`${prefix}Searching \\x1B[36m\"${searchMatch[1]}\"\\x1B[0m`)\n }\n else if (hint.includes('skilld validate')) {\n emit(`${prefix}Validating...`)\n }\n else {\n const shortened = shortenCommand(hint)\n emit(`${prefix}Running ${shortened.length > 50 ? `${shortened.slice(0, 47)}...` : shortened}`)\n }\n }\n else {\n const path = shortenPath(hint || '...')\n emit(`${prefix}${verb} \\x1B[90m${path}\\x1B[0m`)\n }\n }\n }\n}\n\n// ── Per-CLI dispatch ─────────────────────────────────────────────────\n\nconst CLI_DEFS = [claude, gemini, codex] as const\n\nconst CLI_BUILD_ARGS: Record<CliName, (model: string, skillDir: string, symlinkDirs: string[]) => string[]> = {\n claude: claude.buildArgs,\n gemini: gemini.buildArgs,\n codex: codex.buildArgs,\n}\n\nconst CLI_PARSE_LINE: Record<CliName, (line: string) => ParsedEvent> = {\n claude: claude.parseLine,\n gemini: gemini.parseLine,\n codex: codex.parseLine,\n}\n\n// ── Assemble CLI_MODELS from per-CLI model definitions ───────────────\n\nexport const CLI_MODELS: Partial<Record<OptimizeModel, CliModelConfig>> = Object.fromEntries(\n CLI_DEFS.flatMap(def =>\n Object.entries(def.models).map(([id, entry]) => [\n id,\n { ...entry, cli: def.cli, agentId: def.agentId },\n ]),\n ),\n)\n\n// ── Model helpers ────────────────────────────────────────────────────\n\nexport function getModelName(id: OptimizeModel): string {\n return CLI_MODELS[id]?.name ?? id\n}\n\nexport function getModelLabel(id: OptimizeModel): string {\n const config = CLI_MODELS[id]\n if (!config)\n return id\n const agentName = agents[config.agentId]?.displayName ?? config.cli\n return `${agentName} · ${config.name}`\n}\n\nexport async function getAvailableModels(): Promise<import('./types.ts').ModelInfo[]> {\n const execAsync = promisify(exec)\n const lookupCmd = isWindows ? 'where' : 'which'\n\n const installedAgents = detectInstalledAgents()\n const agentsWithCli = installedAgents.filter(id => agents[id].cli)\n\n const cliChecks = await Promise.all(\n agentsWithCli.map(async (agentId) => {\n const cli = agents[agentId].cli!\n try {\n await execAsync(`${lookupCmd} ${cli}`)\n return agentId\n }\n catch { return null }\n }),\n )\n const availableAgentIds = new Set(cliChecks.filter((id): id is AgentType => id != null))\n\n return (Object.entries(CLI_MODELS) as [OptimizeModel, CliModelConfig][])\n .filter(([_, config]) => availableAgentIds.has(config.agentId))\n .map(([id, config]) => ({\n id,\n name: config.name,\n hint: config.hint,\n recommended: config.recommended,\n agentId: config.agentId,\n agentName: agents[config.agentId]?.displayName ?? config.agentId,\n }))\n}\n\n// ── Reference dirs ───────────────────────────────────────────────────\n\n/** Resolve symlinks in .skilld/ to get real paths for --add-dir */\nfunction resolveReferenceDirs(skillDir: string): string[] {\n const refsDir = join(skillDir, '.skilld')\n if (!existsSync(refsDir))\n return []\n const resolved = readdirSync(refsDir)\n .map(entry => join(refsDir, entry))\n .filter(p => lstatSync(p).isSymbolicLink() && existsSync(p))\n .map(p => realpathSync(p))\n\n // Include parent directories so CLIs can search across all references at once\n // (e.g. Gemini's sandbox requires the parent dir to be explicitly included)\n const parents = new Set<string>()\n for (const p of resolved) {\n const parent = dirname(p)\n if (!resolved.includes(parent))\n parents.add(parent)\n }\n\n return [...resolved, ...parents]\n}\n\n// ── Cache ────────────────────────────────────────────────────────────\n\nconst CACHE_DIR = join(homedir(), '.skilld', 'llm-cache')\n\n/** Strip absolute paths from prompt so the hash is project-independent */\nfunction normalizePromptForHash(prompt: string): string {\n return prompt.replace(/\\/[^\\s`]*\\.(?:claude|codex|gemini)\\/skills\\/[^\\s/`]+/g, '<SKILL_DIR>')\n}\n\nfunction hashPrompt(prompt: string, model: OptimizeModel, section: SkillSection): string {\n return createHash('sha256').update(`exec:${model}:${section}:${normalizePromptForHash(prompt)}`).digest('hex').slice(0, 16)\n}\n\nfunction getCached(prompt: string, model: OptimizeModel, section: SkillSection, maxAge = 7 * 24 * 60 * 60 * 1000): string | null {\n const path = join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`)\n if (!existsSync(path))\n return null\n try {\n const { text, timestamp } = JSON.parse(readFileSync(path, 'utf-8'))\n return Date.now() - timestamp > maxAge ? null : text\n }\n catch { return null }\n}\n\nfunction setCache(prompt: string, model: OptimizeModel, section: SkillSection, text: string): void {\n mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 })\n writeFileSync(\n join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`),\n JSON.stringify({ text, model, section, timestamp: Date.now() }),\n { mode: 0o600 },\n )\n}\n\n// ── Per-section spawn ────────────────────────────────────────────────\n\ninterface OptimizeSectionOptions {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n model: OptimizeModel\n packageName: string\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n preExistingFiles: Set<string>\n}\n\n/** Spawn a single CLI process for one section */\nfunction optimizeSection(opts: OptimizeSectionOptions): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug, preExistingFiles } = opts\n\n const cliConfig = CLI_MODELS[model]\n if (!cliConfig) {\n return Promise.resolve({ section, content: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` })\n }\n\n const { cli, model: cliModel } = cliConfig\n const symlinkDirs = resolveReferenceDirs(skillDir)\n const args = CLI_BUILD_ARGS[cli](cliModel, skillDir, symlinkDirs)\n const parseLine = CLI_PARSE_LINE[cli]\n\n const skilldDir = join(skillDir, '.skilld')\n const outputPath = join(skilldDir, outputFile)\n\n // Remove stale output so we don't read a leftover from a previous run\n if (existsSync(outputPath))\n unlinkSync(outputPath)\n\n // Write prompt for debugging\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), prompt)\n\n return new Promise<SectionResult>((resolve) => {\n const proc = spawn(cli, args, {\n cwd: skilldDir,\n stdio: ['pipe', 'pipe', 'pipe'],\n timeout,\n env: { ...process.env, NO_COLOR: '1' },\n shell: isWindows,\n })\n\n let buffer = ''\n let accumulatedText = ''\n let lastWriteContent = ''\n let usage: { input: number, output: number } | undefined\n let cost: number | undefined\n const rawLines: string[] = []\n\n onProgress?.({ chunk: '[starting...]', type: 'reasoning', text: '', reasoning: '', section })\n\n proc.stdin.write(prompt)\n proc.stdin.end()\n\n proc.stdout.on('data', (chunk: Buffer) => {\n buffer += chunk.toString()\n const lines = buffer.split('\\n')\n buffer = lines.pop() || ''\n\n for (const line of lines) {\n if (!line.trim())\n continue\n if (debug)\n rawLines.push(line)\n const evt = parseLine(line)\n\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n\n if (evt.toolName) {\n const hint = evt.toolHint\n ? `[${evt.toolName}: ${evt.toolHint}]`\n : `[${evt.toolName}]`\n onProgress?.({ chunk: hint, type: 'reasoning', text: '', reasoning: hint, section })\n }\n\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n })\n\n let stderr = ''\n proc.stderr.on('data', (chunk: Buffer) => {\n stderr += chunk.toString()\n })\n\n proc.on('close', (code) => {\n // Drain remaining buffer for metadata\n if (buffer.trim()) {\n const evt = parseLine(buffer)\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n\n // Remove unexpected files the LLM may have written (prompt injection defense)\n // Only clean files not in the pre-existing snapshot and not our expected output\n for (const entry of readdirSync(skilldDir)) {\n if (entry !== outputFile && !preExistingFiles.has(entry)) {\n // Allow other section output files and debug prompts\n if (Object.values(SECTION_OUTPUT_FILES).includes(entry))\n continue\n if (entry.startsWith('PROMPT_') || entry === 'logs')\n continue\n try {\n unlinkSync(join(skilldDir, entry))\n }\n catch {}\n }\n }\n\n // Prefer file written by LLM, fall back to Write tool content (if denied), then accumulated stdout\n const raw = (existsSync(outputPath) ? readFileSync(outputPath, 'utf-8') : lastWriteContent || accumulatedText).trim()\n\n // Always write stderr on failure; write all logs in debug mode\n const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n if (debug || (stderr && (!raw || code !== 0))) {\n mkdirSync(logsDir, { recursive: true })\n if (stderr)\n writeFileSync(join(logsDir, `${logName}.stderr.log`), stderr)\n }\n if (debug) {\n mkdirSync(logsDir, { recursive: true })\n if (rawLines.length)\n writeFileSync(join(logsDir, `${logName}.jsonl`), rawLines.join('\\n'))\n if (raw)\n writeFileSync(join(logsDir, `${logName}.md`), raw)\n }\n\n if (!raw && code !== 0) {\n resolve({ section, content: '', wasOptimized: false, error: stderr.trim() || `CLI exited with code ${code}` })\n return\n }\n\n // Clean the section output (strip markdown fences, frontmatter, sanitize)\n const content = raw ? cleanSectionOutput(raw) : ''\n\n if (content) {\n // Write cleaned content back to the output file for debugging\n writeFileSync(outputPath, content)\n }\n\n const validator = getSectionValidator(section)\n const rawWarnings = content && validator ? validator(content) : []\n const warnings: ValidationWarning[] = rawWarnings.map(w => ({ section, warning: w.warning }))\n\n resolve({\n section,\n content,\n wasOptimized: !!content,\n warnings: warnings?.length ? warnings : undefined,\n usage,\n cost,\n })\n })\n\n proc.on('error', (err) => {\n resolve({ section, content: '', wasOptimized: false, error: err.message })\n })\n })\n}\n\n// ── Main orchestrator ────────────────────────────────────────────────\n\nexport async function optimizeDocs(opts: OptimizeDocsOptions): Promise<OptimizeResult> {\n const { packageName, skillDir, model = 'sonnet', version, hasGithub, hasReleases, hasChangelog, docFiles, docsType, hasShippedDocs, onProgress, timeout = 180000, debug, noCache, sections, customPrompt, features, pkgFiles } = opts\n\n const selectedSections = sections ?? ['api-changes', 'best-practices'] as SkillSection[]\n\n // Build all section prompts\n const sectionPrompts = buildAllSectionPrompts({\n packageName,\n skillDir,\n version,\n hasIssues: hasGithub,\n hasDiscussions: hasGithub,\n hasReleases,\n hasChangelog,\n docFiles,\n docsType,\n hasShippedDocs,\n customPrompt,\n features,\n pkgFiles,\n sections: selectedSections,\n })\n\n if (sectionPrompts.size === 0) {\n return { optimized: '', wasOptimized: false, error: 'No valid sections to generate' }\n }\n\n const cliConfig = CLI_MODELS[model]\n if (!cliConfig) {\n return { optimized: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` }\n }\n\n // Check per-section cache: references dir first (version-keyed), then LLM cache (prompt-hashed)\n const cachedResults: SectionResult[] = []\n const uncachedSections: Array<{ section: SkillSection, prompt: string }> = []\n\n for (const [section, prompt] of sectionPrompts) {\n if (!noCache) {\n // Check global references dir (cross-project, version-keyed)\n if (version) {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const refCached = readCachedSection(packageName, version, outputFile)\n if (refCached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: refCached, reasoning: '', section })\n cachedResults.push({ section, content: refCached, wasOptimized: true })\n continue\n }\n }\n\n // Check LLM prompt-hash cache\n const cached = getCached(prompt, model, section)\n if (cached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: cached, reasoning: '', section })\n cachedResults.push({ section, content: cached, wasOptimized: true })\n continue\n }\n }\n uncachedSections.push({ section, prompt })\n }\n\n // Prepare .skilld/ dir and snapshot before spawns\n const skilldDir = join(skillDir, '.skilld')\n mkdirSync(skilldDir, { recursive: true })\n\n // Pre-flight: warn about broken symlinks in .skilld/ (avoids wasting tokens on missing refs)\n for (const entry of readdirSync(skilldDir)) {\n const entryPath = join(skilldDir, entry)\n try {\n if (lstatSync(entryPath).isSymbolicLink() && !existsSync(entryPath))\n onProgress?.({ chunk: `[warn: broken symlink .skilld/${entry}]`, type: 'reasoning', text: '', reasoning: '' })\n }\n catch {}\n }\n\n const preExistingFiles = new Set(readdirSync(skilldDir))\n\n // Spawn uncached sections with staggered starts to avoid rate-limit collisions\n const STAGGER_MS = 3000\n const spawnResults = uncachedSections.length > 0\n ? await Promise.allSettled(\n uncachedSections.map(({ section, prompt }, i) => {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const run = () => optimizeSection({\n section,\n prompt,\n outputFile,\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n })\n // Stagger: first section starts immediately, rest delayed\n if (i === 0)\n return run()\n return delay(i * STAGGER_MS).then(run)\n }),\n )\n : []\n\n // Collect results, retry failed sections once\n const allResults: SectionResult[] = [...cachedResults]\n let totalUsage: { input: number, output: number } | undefined\n let totalCost = 0\n const retryQueue: Array<{ index: number, section: SkillSection, prompt: string }> = []\n\n for (let i = 0; i < spawnResults.length; i++) {\n const r = spawnResults[i]!\n const { section, prompt } = uncachedSections[i]!\n if (r.status === 'fulfilled' && r.value.wasOptimized) {\n allResults.push(r.value)\n if (r.value.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += r.value.usage.input\n totalUsage.output += r.value.usage.output\n }\n if (r.value.cost != null)\n totalCost += r.value.cost\n if (!noCache)\n setCache(prompt, model, section, r.value.content)\n }\n else {\n retryQueue.push({ index: i, section, prompt })\n }\n }\n\n // Retry failed sections once (sequential to avoid rate limits)\n for (const { section, prompt } of retryQueue) {\n onProgress?.({ chunk: `[${section}: retrying...]`, type: 'reasoning', text: '', reasoning: '', section })\n await delay(STAGGER_MS)\n const result = await optimizeSection({\n section,\n prompt,\n outputFile: SECTION_OUTPUT_FILES[section],\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n }).catch((err: Error) => ({ section, content: '', wasOptimized: false, error: err.message }) as SectionResult)\n\n allResults.push(result)\n if (result.wasOptimized && !noCache)\n setCache(prompt, model, section, result.content)\n if (result.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += result.usage.input\n totalUsage.output += result.usage.output\n }\n if (result.cost != null)\n totalCost += result.cost\n }\n\n // Write successful sections to global references dir for cross-project reuse\n if (version) {\n const sectionFiles = allResults\n .filter(r => r.wasOptimized && r.content)\n .map(r => ({ file: SECTION_OUTPUT_FILES[r.section], content: r.content }))\n if (sectionFiles.length > 0) {\n writeSections(packageName, version, sectionFiles)\n }\n }\n\n // Merge results in SECTION_MERGE_ORDER\n const mergedParts: string[] = []\n for (const section of SECTION_MERGE_ORDER) {\n const result = allResults.find(r => r.section === section)\n if (result?.wasOptimized && result.content) {\n mergedParts.push(result.content)\n }\n }\n\n const optimized = mergedParts.join('\\n\\n')\n const wasOptimized = mergedParts.length > 0\n\n const usageResult = totalUsage\n ? { inputTokens: totalUsage.input, outputTokens: totalUsage.output, totalTokens: totalUsage.input + totalUsage.output }\n : undefined\n\n // Collect errors and warnings from sections\n const errors = allResults.filter(r => r.error).map(r => `${r.section}: ${r.error}`)\n const warnings = allResults.flatMap(r => r.warnings ?? []).map(w => `${w.section}: ${w.warning}`)\n\n const debugLogsDir = debug && uncachedSections.length > 0\n ? join(skillDir, '.skilld', 'logs')\n : undefined\n\n return {\n optimized,\n wasOptimized,\n error: errors.length > 0 ? errors.join('; ') : undefined,\n warnings: warnings.length > 0 ? warnings : undefined,\n finishReason: wasOptimized ? 'stop' : 'error',\n usage: usageResult,\n cost: totalCost || undefined,\n debugLogsDir,\n }\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────\n\n/** Shorten absolute paths for display: /home/user/project/.claude/skills/vue/SKILL.md → .claude/.../SKILL.md */\nfunction shortenPath(p: string): string {\n const refIdx = p.indexOf('.skilld/')\n if (refIdx !== -1)\n return p.slice(refIdx + '.skilld/'.length)\n // Keep just filename for other paths\n const parts = p.split('/')\n return parts.length > 2 ? `.../${parts.slice(-2).join('/')}` : p\n}\n\n/** Replace absolute paths in a command string with shortened versions */\nfunction shortenCommand(cmd: string): string {\n return cmd.replace(/\\/[^\\s\"']+/g, (match) => {\n // Only shorten paths that look like they're inside a project\n if (match.includes('.claude/') || match.includes('.skilld/') || match.includes('node_modules/'))\n return `.../${match.split('/').slice(-2).join('/')}`\n return match\n })\n}\n\n/** Clean a single section's LLM output: strip markdown fences, frontmatter, sanitize */\nexport function cleanSectionOutput(content: string): string {\n let cleaned = content.trim()\n\n // Strip wrapping fences if output is wrapped in ```markdown, ```md, or bare ```\n // Requires matched open+close pair to avoid stripping internal code blocks\n const wrapMatch = cleaned.match(/^```(?:markdown|md)?[^\\S\\n]*\\n([\\s\\S]+)\\n```[^\\S\\n]*$/)\n if (wrapMatch) {\n const inner = wrapMatch[1]!.trim()\n // For bare ``` wrappers (no markdown/md tag), verify inner looks like section output\n const isExplicitWrapper = /^```(?:markdown|md)/.test(cleaned)\n if (isExplicitWrapper || /^##\\s/m.test(inner) || /^- (?:BREAKING|DEPRECATED|NEW): /m.test(inner)) {\n cleaned = inner\n }\n }\n\n // Normalize h1 headers to h2 — LLMs sometimes use # instead of ##\n cleaned = cleaned.replace(/^# (?!#)/gm, '## ')\n\n // Strip accidental frontmatter or leading horizontal rules\n const fmMatch = cleaned.match(/^-{3,}\\n/)\n if (fmMatch) {\n const afterOpen = fmMatch[0].length\n const closeMatch = cleaned.slice(afterOpen).match(/\\n-{3,}/)\n if (closeMatch) {\n cleaned = cleaned.slice(afterOpen + closeMatch.index! + closeMatch[0].length).trim()\n }\n else {\n cleaned = cleaned.slice(afterOpen).trim()\n }\n }\n\n // Strip raw code preamble before first section marker (defense against LLMs dumping source)\n // Section markers: ## heading, BREAKING/DEPRECATED/NEW labels\n const firstMarker = cleaned.match(/^(##\\s|- (?:BREAKING|DEPRECATED|NEW): )/m)\n if (firstMarker?.index && firstMarker.index > 0) {\n const preamble = cleaned.slice(0, firstMarker.index)\n // Only strip if preamble looks like code (contains function/const/export/return patterns)\n if (/\\b(?:function|const |let |var |export |return |import |async |class )\\b/.test(preamble)) {\n cleaned = cleaned.slice(firstMarker.index).trim()\n }\n }\n\n // Strip duplicate section headings (LLM echoing the format example before real content)\n // Handles headings separated by blank lines or boilerplate text\n const headingMatch = cleaned.match(/^(## .+)\\n/)\n if (headingMatch) {\n const heading = headingMatch[1]!\n const afterFirst = headingMatch[0].length\n const secondIdx = cleaned.indexOf(heading, afterFirst)\n if (secondIdx !== -1) {\n // Only strip if the gap between duplicates is small (< 200 chars of boilerplate)\n if (secondIdx - afterFirst < 200)\n cleaned = cleaned.slice(secondIdx).trim()\n }\n }\n\n // Normalize source link paths: ensure .skilld/ prefix is present\n // LLMs sometimes emit [source](./docs/...) instead of [source](./.skilld/docs/...)\n cleaned = cleaned.replace(\n /\\[source\\]\\(\\.\\/((docs|issues|discussions|releases|pkg|guide)\\/)/g,\n '[source](./.skilld/$1',\n )\n\n cleaned = sanitizeMarkdown(cleaned)\n\n // Reject content that lacks any section structure — likely leaked LLM reasoning/narration\n // Valid sections contain headings (##), API change labels, or source-linked items\n if (!/^##\\s/m.test(cleaned) && !/^- (?:BREAKING|DEPRECATED|NEW): /m.test(cleaned) && !/\\[source\\]/.test(cleaned)) {\n return ''\n }\n\n return cleaned\n}\n","/**\n * Detect packages from framework presets (e.g., Nuxt modules in nuxt.config)\n * These are string literals in config arrays, not imports — the import scanner misses them.\n */\n\nimport type { PackageUsage } from './detect-imports.ts'\nimport { readFile } from 'node:fs/promises'\nimport { parseSync } from 'oxc-parser'\nimport { join } from 'pathe'\n\nconst NUXT_CONFIG_FILES = ['nuxt.config.ts', 'nuxt.config.js', 'nuxt.config.mjs']\nconst NUXT_ECOSYSTEM = ['vue', 'nitro', 'h3']\n\nasync function findNuxtConfig(cwd: string): Promise<{ path: string, content: string } | null> {\n for (const name of NUXT_CONFIG_FILES) {\n const path = join(cwd, name)\n const content = await readFile(path, 'utf8').catch(() => null)\n if (content)\n return { path, content }\n }\n return null\n}\n\n/**\n * Walk AST node to find all string values inside a `modules` array property.\n * Handles: defineNuxtConfig({ modules: [...] }) and export default { modules: [...] }\n */\nexport function extractModuleStrings(node: any): string[] {\n if (!node || typeof node !== 'object')\n return []\n\n // Found a Property with key \"modules\" and an ArrayExpression value\n if (node.type === 'Property' && !node.computed\n && (node.key?.type === 'Identifier' && node.key.name === 'modules')\n && node.value?.type === 'ArrayExpression') { return node.value.elements.filter((el: any) => el?.type === 'Literal' && typeof el.value === 'string').map((el: any) => el.value as string) }\n\n // Recurse into arrays and object values\n const results: string[] = []\n if (Array.isArray(node)) {\n for (const child of node)\n results.push(...extractModuleStrings(child))\n }\n else {\n for (const key of Object.keys(node)) {\n if (key === 'start' || key === 'end' || key === 'type')\n continue\n const val = node[key]\n if (val && typeof val === 'object')\n results.push(...extractModuleStrings(val))\n }\n }\n return results\n}\n\n/**\n * Detect Nuxt modules from nuxt.config.{ts,js,mjs}\n */\nexport async function detectNuxtModules(cwd: string): Promise<PackageUsage[]> {\n const config = await findNuxtConfig(cwd)\n if (!config)\n return []\n\n const result = parseSync(config.path, config.content)\n const modules = extractModuleStrings(result.program)\n\n // Dedupe and build results\n const seen = new Set<string>()\n const packages: PackageUsage[] = []\n\n for (const mod of modules) {\n if (!seen.has(mod)) {\n seen.add(mod)\n packages.push({ name: mod, count: 0, source: 'preset' })\n }\n }\n\n // Add core ecosystem packages\n for (const pkg of NUXT_ECOSYSTEM) {\n if (!seen.has(pkg)) {\n seen.add(pkg)\n packages.push({ name: pkg, count: 0, source: 'preset' })\n }\n }\n\n return packages\n}\n\n/**\n * Run all preset detectors and merge results\n */\nexport async function detectPresetPackages(cwd: string): Promise<PackageUsage[]> {\n // Currently only Nuxt, but extensible for other frameworks\n return detectNuxtModules(cwd)\n}\n","/**\n * Detect directly-used npm packages by scanning source files\n * Uses mlly for proper ES module parsing + tinyglobby for file discovery\n */\n\nimport { readFile } from 'node:fs/promises'\nimport { findDynamicImports, findStaticImports } from 'mlly'\nimport { glob } from 'tinyglobby'\nimport { detectPresetPackages } from './detect-presets.ts'\n\nexport interface PackageUsage {\n name: string\n count: number\n source?: 'import' | 'preset'\n}\n\nexport interface DetectResult {\n packages: PackageUsage[]\n error?: string\n}\n\nconst PATTERNS = ['**/*.{ts,js,vue,mjs,cjs,tsx,jsx,mts,cts}']\nconst IGNORE = ['**/node_modules/**', '**/dist/**', '**/.nuxt/**', '**/.output/**', '**/coverage/**']\n\nfunction addPackage(counts: Map<string, number>, specifier: string | undefined) {\n if (!specifier || specifier.startsWith('.') || specifier.startsWith('/'))\n return\n\n // Extract package name (handle subpaths like 'pkg/subpath')\n const name = specifier.startsWith('@')\n ? specifier.split('/').slice(0, 2).join('/')\n : specifier.split('/')[0]!\n\n if (!isNodeBuiltin(name)) {\n counts.set(name, (counts.get(name) || 0) + 1)\n }\n}\n\n/**\n * Scan source files to detect all directly-imported npm packages\n * Async with gitignore support for proper spinner animation\n */\nexport async function detectImportedPackages(cwd: string = process.cwd()): Promise<DetectResult> {\n try {\n const counts = new Map<string, number>()\n\n const files = await glob(PATTERNS, {\n cwd,\n ignore: IGNORE,\n absolute: true,\n expandDirectories: false,\n })\n\n await Promise.all(files.map(async (file) => {\n const content = await readFile(file, 'utf8')\n\n // Static: import x from 'pkg'\n for (const imp of findStaticImports(content)) {\n addPackage(counts, imp.specifier)\n }\n\n // Dynamic: import('pkg') - expression is the string literal\n for (const imp of findDynamicImports(content)) {\n // expression includes quotes, extract string value\n const match = imp.expression.match(/^['\"]([^'\"]+)['\"]$/)\n if (match)\n addPackage(counts, match[1]!)\n }\n }))\n\n // Sort by usage count (descending), then alphabetically\n const packages: PackageUsage[] = [...counts.entries()]\n .map(([name, count]) => ({ name, count, source: 'import' as const }))\n .sort((a, b) => b.count - a.count || a.name.localeCompare(b.name))\n\n // Merge preset-detected packages (imports take priority)\n const presets = await detectPresetPackages(cwd)\n const importNames = new Set(packages.map(p => p.name))\n for (const preset of presets) {\n if (!importNames.has(preset.name))\n packages.push(preset)\n }\n\n return { packages }\n }\n catch (err) {\n return { packages: [], error: String(err) }\n }\n}\n\nconst NODE_BUILTINS = new Set([\n 'assert',\n 'buffer',\n 'child_process',\n 'cluster',\n 'console',\n 'constants',\n 'crypto',\n 'dgram',\n 'dns',\n 'domain',\n 'events',\n 'fs',\n 'http',\n 'https',\n 'module',\n 'net',\n 'os',\n 'path',\n 'perf_hooks',\n 'process',\n 'punycode',\n 'querystring',\n 'readline',\n 'repl',\n 'stream',\n 'string_decoder',\n 'sys',\n 'timers',\n 'tls',\n 'tty',\n 'url',\n 'util',\n 'v8',\n 'vm',\n 'wasi',\n 'worker_threads',\n 'zlib',\n])\n\nfunction isNodeBuiltin(pkg: string): boolean {\n const base = pkg.startsWith('node:') ? pkg.slice(5) : pkg\n return NODE_BUILTINS.has(base.split('/')[0]!)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAaA,QAAM;AACnB,MAAaC,YAAU;AAEv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C;AAEF,SAAS,cAAc,KAAsB;CAC3C,MAAM,OAAO,IAAI,WAAW,QAAQ,GAAG,IAAI,MAAM,EAAE,GAAG;AACtD,QAAO,cAAc,IAAI,KAAK,MAAM,IAAI,CAAC,GAAI"}
|
|
@@ -2,6 +2,7 @@ import { a as getRepoCacheDir, n as REFERENCES_DIR, o as getCacheDir, r as REPOS
|
|
|
2
2
|
import { n as sanitizeMarkdown } from "./sanitize.mjs";
|
|
3
3
|
import { basename, join, resolve } from "pathe";
|
|
4
4
|
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
5
|
+
/** Safely create a symlink, validating target is under REFERENCES_DIR or REPOS_DIR */
|
|
5
6
|
function safeSymlink(target, linkPath) {
|
|
6
7
|
const resolved = resolve(target);
|
|
7
8
|
if (!resolved.startsWith(REFERENCES_DIR) && !resolved.startsWith(REPOS_DIR)) throw new Error(`Symlink target outside allowed dirs: ${resolved}`);
|
|
@@ -11,9 +12,15 @@ function safeSymlink(target, linkPath) {
|
|
|
11
12
|
} catch {}
|
|
12
13
|
symlinkSync(target, linkPath, "junction");
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if package is cached at given version
|
|
17
|
+
*/
|
|
14
18
|
function isCached(name, version) {
|
|
15
19
|
return existsSync(getCacheDir(name, version));
|
|
16
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Ensure cache directories exist
|
|
23
|
+
*/
|
|
17
24
|
function ensureCacheDir() {
|
|
18
25
|
mkdirSync(REFERENCES_DIR, {
|
|
19
26
|
recursive: true,
|
|
@@ -24,6 +31,9 @@ function ensureCacheDir() {
|
|
|
24
31
|
mode: 448
|
|
25
32
|
});
|
|
26
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Write docs to cache
|
|
36
|
+
*/
|
|
27
37
|
function writeToCache(name, version, docs) {
|
|
28
38
|
const cacheDir = getCacheDir(name, version);
|
|
29
39
|
mkdirSync(cacheDir, {
|
|
@@ -40,6 +50,9 @@ function writeToCache(name, version, docs) {
|
|
|
40
50
|
}
|
|
41
51
|
return cacheDir;
|
|
42
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Write docs to repo-level cache (~/.skilld/repos/<owner>/<repo>/)
|
|
55
|
+
*/
|
|
43
56
|
function writeToRepoCache(owner, repo, docs) {
|
|
44
57
|
const repoDir = getRepoCacheDir(owner, repo);
|
|
45
58
|
mkdirSync(repoDir, {
|
|
@@ -56,6 +69,10 @@ function writeToRepoCache(owner, repo, docs) {
|
|
|
56
69
|
}
|
|
57
70
|
return repoDir;
|
|
58
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Create symlink from .skilld dir to a repo-level cached subdirectory.
|
|
74
|
+
* .claude/skills/<skill>/.skilld/<subdir> -> ~/.skilld/repos/<owner>/<repo>/<subdir>
|
|
75
|
+
*/
|
|
59
76
|
function linkRepoCachedDir(skillDir, owner, repo, subdir) {
|
|
60
77
|
const repoDir = getRepoCacheDir(owner, repo);
|
|
61
78
|
const referencesDir = join(skillDir, ".skilld");
|
|
@@ -64,6 +81,15 @@ function linkRepoCachedDir(skillDir, owner, repo, subdir) {
|
|
|
64
81
|
mkdirSync(referencesDir, { recursive: true });
|
|
65
82
|
if (existsSync(cachedPath)) safeSymlink(cachedPath, linkPath);
|
|
66
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Create symlink from .skilld dir to a cached subdirectory.
|
|
86
|
+
* Unified handler for docs, issues, discussions, sections, releases.
|
|
87
|
+
*
|
|
88
|
+
* Structure:
|
|
89
|
+
* .claude/skills/<skill>/.skilld/<subdir> -> ~/.skilld/references/<pkg>@<version>/<subdir>
|
|
90
|
+
*
|
|
91
|
+
* The .skilld/ dirs are gitignored. After clone, `skilld install` recreates from lockfile.
|
|
92
|
+
*/
|
|
67
93
|
function linkCachedDir(skillDir, name, version, subdir) {
|
|
68
94
|
const cacheDir = getCacheDir(name, version);
|
|
69
95
|
const referencesDir = join(skillDir, ".skilld");
|
|
@@ -72,6 +98,10 @@ function linkCachedDir(skillDir, name, version, subdir) {
|
|
|
72
98
|
mkdirSync(referencesDir, { recursive: true });
|
|
73
99
|
if (existsSync(cachedPath)) safeSymlink(cachedPath, linkPath);
|
|
74
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Resolve the package directory: node_modules first, then cached dist fallback.
|
|
103
|
+
* Returns the path if found, null otherwise.
|
|
104
|
+
*/
|
|
75
105
|
function resolvePkgDir(name, cwd, version) {
|
|
76
106
|
const nodeModulesPath = join(cwd, "node_modules", name);
|
|
77
107
|
if (existsSync(nodeModulesPath)) return nodeModulesPath;
|
|
@@ -81,6 +111,14 @@ function resolvePkgDir(name, cwd, version) {
|
|
|
81
111
|
}
|
|
82
112
|
return null;
|
|
83
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Create symlink from .skilld dir to package directory
|
|
116
|
+
*
|
|
117
|
+
* Structure:
|
|
118
|
+
* .claude/skills/<skill>/.skilld/pkg -> node_modules/<pkg> OR ~/.skilld/references/<pkg>@<version>/pkg
|
|
119
|
+
*
|
|
120
|
+
* This gives access to package.json, README.md, dist/, and any shipped docs/
|
|
121
|
+
*/
|
|
84
122
|
function linkPkg(skillDir, name, cwd, version) {
|
|
85
123
|
const pkgPath = resolvePkgDir(name, cwd, version);
|
|
86
124
|
if (!pkgPath) return;
|
|
@@ -90,6 +128,13 @@ function linkPkg(skillDir, name, cwd, version) {
|
|
|
90
128
|
if (existsSync(pkgLinkPath)) unlinkSync(pkgLinkPath);
|
|
91
129
|
symlinkSync(pkgPath, pkgLinkPath, "junction");
|
|
92
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Create named symlink from .skilld dir to package directory.
|
|
133
|
+
* Short name = last segment of package name (e.g., @vue/reactivity → pkg-reactivity)
|
|
134
|
+
*
|
|
135
|
+
* Structure:
|
|
136
|
+
* .claude/skills/<skill>/.skilld/pkg-<short> -> node_modules/<pkg>
|
|
137
|
+
*/
|
|
93
138
|
function linkPkgNamed(skillDir, name, cwd, version) {
|
|
94
139
|
const pkgPath = resolvePkgDir(name, cwd, version);
|
|
95
140
|
if (!pkgPath) return;
|
|
@@ -100,6 +145,10 @@ function linkPkgNamed(skillDir, name, cwd, version) {
|
|
|
100
145
|
if (existsSync(linkPath)) unlinkSync(linkPath);
|
|
101
146
|
symlinkSync(pkgPath, linkPath, "junction");
|
|
102
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Get key files from a package directory for display
|
|
150
|
+
* Returns entry points + docs files
|
|
151
|
+
*/
|
|
103
152
|
function getPkgKeyFiles(name, cwd, version) {
|
|
104
153
|
const pkgPath = resolvePkgDir(name, cwd, version);
|
|
105
154
|
if (!pkgPath) return [];
|
|
@@ -116,6 +165,9 @@ function getPkgKeyFiles(name, cwd, version) {
|
|
|
116
165
|
files.push(...entries);
|
|
117
166
|
return [...new Set(files)];
|
|
118
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Check if package ships a skills/ directory with SKILL.md or _SKILL.md subdirs
|
|
170
|
+
*/
|
|
119
171
|
function getShippedSkills(name, cwd, version) {
|
|
120
172
|
const pkgPath = resolvePkgDir(name, cwd, version);
|
|
121
173
|
if (!pkgPath) return [];
|
|
@@ -126,6 +178,12 @@ function getShippedSkills(name, cwd, version) {
|
|
|
126
178
|
skillDir: join(skillsPath, d.name)
|
|
127
179
|
}));
|
|
128
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Write LLM-generated section outputs to global cache for cross-project reuse
|
|
183
|
+
*
|
|
184
|
+
* Structure:
|
|
185
|
+
* ~/.skilld/references/<pkg>@<version>/sections/_BEST_PRACTICES.md
|
|
186
|
+
*/
|
|
129
187
|
function writeSections(name, version, sections) {
|
|
130
188
|
const sectionsDir = join(getCacheDir(name, version), "sections");
|
|
131
189
|
mkdirSync(sectionsDir, {
|
|
@@ -134,11 +192,17 @@ function writeSections(name, version, sections) {
|
|
|
134
192
|
});
|
|
135
193
|
for (const { file, content } of sections) writeFileSync(join(sectionsDir, file), content, { mode: 384 });
|
|
136
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Read a cached section from the global references dir
|
|
197
|
+
*/
|
|
137
198
|
function readCachedSection(name, version, file) {
|
|
138
199
|
const path = join(getCacheDir(name, version), "sections", file);
|
|
139
200
|
if (!existsSync(path)) return null;
|
|
140
201
|
return readFileSync(path, "utf-8");
|
|
141
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Create symlink from skills dir to shipped skill dir
|
|
205
|
+
*/
|
|
142
206
|
function linkShippedSkill(baseDir, skillName, targetDir) {
|
|
143
207
|
const linkPath = join(baseDir, skillName);
|
|
144
208
|
if (existsSync(linkPath)) if (lstatSync(linkPath).isSymbolicLink()) unlinkSync(linkPath);
|
|
@@ -158,6 +222,9 @@ function hasShippedDocs(name, cwd, version) {
|
|
|
158
222
|
]) if (existsSync(join(pkgPath, candidate))) return true;
|
|
159
223
|
return false;
|
|
160
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* List all cached packages
|
|
227
|
+
*/
|
|
161
228
|
function listCached() {
|
|
162
229
|
if (!existsSync(REFERENCES_DIR)) return [];
|
|
163
230
|
return readdirSync(REFERENCES_DIR).filter((name) => name.includes("@")).map((dir) => {
|
|
@@ -169,6 +236,9 @@ function listCached() {
|
|
|
169
236
|
};
|
|
170
237
|
});
|
|
171
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Read cached docs for a package
|
|
241
|
+
*/
|
|
172
242
|
function readCachedDocs(name, version) {
|
|
173
243
|
const cacheDir = getCacheDir(name, version);
|
|
174
244
|
if (!existsSync(cacheDir)) return [];
|
|
@@ -187,18 +257,28 @@ function readCachedDocs(name, version) {
|
|
|
187
257
|
walk(cacheDir);
|
|
188
258
|
return docs;
|
|
189
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Clear cache for a specific package
|
|
262
|
+
*/
|
|
190
263
|
function clearCache(name, version) {
|
|
191
264
|
const cacheDir = getCacheDir(name, version);
|
|
192
265
|
if (!existsSync(cacheDir)) return false;
|
|
193
266
|
rmSync(cacheDir, { recursive: true });
|
|
194
267
|
return true;
|
|
195
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Clear all cache
|
|
271
|
+
*/
|
|
196
272
|
function clearAllCache() {
|
|
197
273
|
const packages = listCached();
|
|
198
274
|
for (const pkg of packages) clearCache(pkg.name, pkg.version);
|
|
199
275
|
if (existsSync(REPOS_DIR)) rmSync(REPOS_DIR, { recursive: true });
|
|
200
276
|
return packages.length;
|
|
201
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* List files in .skilld directory (pkg + docs) as relative paths for prompt context
|
|
280
|
+
* Returns paths like ./.skilld/pkg/README.md, ./.skilld/docs/api.md
|
|
281
|
+
*/
|
|
202
282
|
function listReferenceFiles(skillDir, maxDepth = 3) {
|
|
203
283
|
const referencesDir = join(skillDir, ".skilld");
|
|
204
284
|
if (!existsSync(referencesDir)) return [];
|
|
@@ -225,4 +305,4 @@ function listReferenceFiles(skillDir, maxDepth = 3) {
|
|
|
225
305
|
}
|
|
226
306
|
export { resolvePkgDir as _, getShippedSkills as a, writeToRepoCache as b, linkCachedDir as c, linkRepoCachedDir as d, linkShippedSkill as f, readCachedSection as g, readCachedDocs as h, getPkgKeyFiles as i, linkPkg as l, listReferenceFiles as m, clearCache as n, hasShippedDocs as o, listCached as p, ensureCacheDir as r, isCached as s, clearAllCache as t, linkPkgNamed as u, writeSections as v, writeToCache as y };
|
|
227
307
|
|
|
228
|
-
//# sourceMappingURL=
|
|
308
|
+
//# sourceMappingURL=cache.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.mjs","names":[],"sources":["../../src/cache/storage.ts"],"sourcesContent":["/**\n * Cache storage operations\n */\n\nimport type { CachedDoc, CachedPackage } from './types.ts'\nimport { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs'\nimport { basename, join, resolve } from 'pathe'\nimport { sanitizeMarkdown } from '../core/sanitize.ts'\nimport { getRepoCacheDir, REFERENCES_DIR, REPOS_DIR } from './config.ts'\nimport { getCacheDir } from './version.ts'\n\n/** Safely create a symlink, validating target is under REFERENCES_DIR or REPOS_DIR */\nfunction safeSymlink(target: string, linkPath: string): void {\n const resolved = resolve(target)\n if (!resolved.startsWith(REFERENCES_DIR) && !resolved.startsWith(REPOS_DIR))\n throw new Error(`Symlink target outside allowed dirs: ${resolved}`)\n // Remove pre-existing symlink (check with lstat to detect symlinks)\n try {\n const stat = lstatSync(linkPath)\n if (stat.isSymbolicLink() || stat.isFile())\n unlinkSync(linkPath)\n }\n catch {}\n symlinkSync(target, linkPath, 'junction')\n}\n\n/**\n * Check if package is cached at given version\n */\nexport function isCached(name: string, version: string): boolean {\n return existsSync(getCacheDir(name, version))\n}\n\n/**\n * Ensure cache directories exist\n */\nexport function ensureCacheDir(): void {\n mkdirSync(REFERENCES_DIR, { recursive: true, mode: 0o700 })\n mkdirSync(REPOS_DIR, { recursive: true, mode: 0o700 })\n}\n\n/**\n * Write docs to cache\n */\nexport function writeToCache(\n name: string,\n version: string,\n docs: CachedDoc[],\n): string {\n const cacheDir = getCacheDir(name, version)\n mkdirSync(cacheDir, { recursive: true, mode: 0o700 })\n\n for (const doc of docs) {\n const filePath = join(cacheDir, doc.path)\n mkdirSync(join(filePath, '..'), { recursive: true, mode: 0o700 })\n writeFileSync(filePath, sanitizeMarkdown(doc.content), { mode: 0o600 })\n }\n\n return cacheDir\n}\n\n/**\n * Write docs to repo-level cache (~/.skilld/repos/<owner>/<repo>/)\n */\nexport function writeToRepoCache(\n owner: string,\n repo: string,\n docs: CachedDoc[],\n): string {\n const repoDir = getRepoCacheDir(owner, repo)\n mkdirSync(repoDir, { recursive: true, mode: 0o700 })\n\n for (const doc of docs) {\n const filePath = join(repoDir, doc.path)\n mkdirSync(join(filePath, '..'), { recursive: true, mode: 0o700 })\n writeFileSync(filePath, sanitizeMarkdown(doc.content), { mode: 0o600 })\n }\n\n return repoDir\n}\n\n/**\n * Create symlink from .skilld dir to a repo-level cached subdirectory.\n * .claude/skills/<skill>/.skilld/<subdir> -> ~/.skilld/repos/<owner>/<repo>/<subdir>\n */\nexport function linkRepoCachedDir(skillDir: string, owner: string, repo: string, subdir: string): void {\n const repoDir = getRepoCacheDir(owner, repo)\n const referencesDir = join(skillDir, '.skilld')\n const linkPath = join(referencesDir, subdir)\n const cachedPath = join(repoDir, subdir)\n\n mkdirSync(referencesDir, { recursive: true })\n\n if (existsSync(cachedPath)) {\n safeSymlink(cachedPath, linkPath)\n }\n}\n\n/**\n * Create symlink from .skilld dir to a cached subdirectory.\n * Unified handler for docs, issues, discussions, sections, releases.\n *\n * Structure:\n * .claude/skills/<skill>/.skilld/<subdir> -> ~/.skilld/references/<pkg>@<version>/<subdir>\n *\n * The .skilld/ dirs are gitignored. After clone, `skilld install` recreates from lockfile.\n */\nexport function linkCachedDir(skillDir: string, name: string, version: string, subdir: string): void {\n const cacheDir = getCacheDir(name, version)\n const referencesDir = join(skillDir, '.skilld')\n const linkPath = join(referencesDir, subdir)\n const cachedPath = join(cacheDir, subdir)\n\n mkdirSync(referencesDir, { recursive: true })\n\n if (existsSync(cachedPath)) {\n safeSymlink(cachedPath, linkPath)\n }\n}\n\n/**\n * Resolve the package directory: node_modules first, then cached dist fallback.\n * Returns the path if found, null otherwise.\n */\nexport function resolvePkgDir(name: string, cwd: string, version?: string): string | null {\n const nodeModulesPath = join(cwd, 'node_modules', name)\n if (existsSync(nodeModulesPath))\n return nodeModulesPath\n\n // Fallback: check cached npm dist\n if (version) {\n const cachedPkgDir = join(getCacheDir(name, version), 'pkg')\n if (existsSync(join(cachedPkgDir, 'package.json')))\n return cachedPkgDir\n }\n\n return null\n}\n\n/**\n * Create symlink from .skilld dir to package directory\n *\n * Structure:\n * .claude/skills/<skill>/.skilld/pkg -> node_modules/<pkg> OR ~/.skilld/references/<pkg>@<version>/pkg\n *\n * This gives access to package.json, README.md, dist/, and any shipped docs/\n */\nexport function linkPkg(skillDir: string, name: string, cwd: string, version?: string): void {\n const pkgPath = resolvePkgDir(name, cwd, version)\n if (!pkgPath)\n return\n\n const referencesDir = join(skillDir, '.skilld')\n mkdirSync(referencesDir, { recursive: true })\n\n const pkgLinkPath = join(referencesDir, 'pkg')\n if (existsSync(pkgLinkPath)) {\n unlinkSync(pkgLinkPath)\n }\n symlinkSync(pkgPath, pkgLinkPath, 'junction')\n}\n\n/**\n * Create named symlink from .skilld dir to package directory.\n * Short name = last segment of package name (e.g., @vue/reactivity → pkg-reactivity)\n *\n * Structure:\n * .claude/skills/<skill>/.skilld/pkg-<short> -> node_modules/<pkg>\n */\nexport function linkPkgNamed(skillDir: string, name: string, cwd: string, version?: string): void {\n const pkgPath = resolvePkgDir(name, cwd, version)\n if (!pkgPath)\n return\n\n const shortName = name.split('/').pop()!.toLowerCase()\n const referencesDir = join(skillDir, '.skilld')\n mkdirSync(referencesDir, { recursive: true })\n\n const linkPath = join(referencesDir, `pkg-${shortName}`)\n if (existsSync(linkPath))\n unlinkSync(linkPath)\n symlinkSync(pkgPath, linkPath, 'junction')\n}\n\n/**\n * Get key files from a package directory for display\n * Returns entry points + docs files\n */\nexport function getPkgKeyFiles(name: string, cwd: string, version?: string): string[] {\n const pkgPath = resolvePkgDir(name, cwd, version)\n if (!pkgPath)\n return []\n\n const files: string[] = []\n const pkgJsonPath = join(pkgPath, 'package.json')\n\n if (existsSync(pkgJsonPath)) {\n const pkg = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'))\n\n // Entry points\n if (pkg.main)\n files.push(basename(pkg.main))\n if (pkg.module && pkg.module !== pkg.main)\n files.push(basename(pkg.module))\n\n // Type definitions (relative path preserved for LLM tool hints)\n const typesPath = pkg.types || pkg.typings\n if (typesPath && existsSync(join(pkgPath, typesPath)))\n files.push(typesPath)\n }\n\n // Check for common doc files (case-insensitive readme match)\n const entries = readdirSync(pkgPath).filter(f =>\n /^readme\\.md$/i.test(f) || /^changelog\\.md$/i.test(f),\n )\n files.push(...entries)\n\n return [...new Set(files)]\n}\n\n/**\n * Check if package ships its own docs folder\n */\nexport interface ShippedSkill {\n skillName: string\n skillDir: string\n}\n\n/**\n * Check if package ships a skills/ directory with SKILL.md or _SKILL.md subdirs\n */\nexport function getShippedSkills(name: string, cwd: string, version?: string): ShippedSkill[] {\n const pkgPath = resolvePkgDir(name, cwd, version)\n if (!pkgPath)\n return []\n\n const skillsPath = join(pkgPath, 'skills')\n if (!existsSync(skillsPath))\n return []\n\n return readdirSync(skillsPath, { withFileTypes: true })\n .filter(d => d.isDirectory() && (existsSync(join(skillsPath, d.name, 'SKILL.md')) || existsSync(join(skillsPath, d.name, '_SKILL.md'))))\n .map(d => ({ skillName: d.name, skillDir: join(skillsPath, d.name) }))\n}\n\n/**\n * Write LLM-generated section outputs to global cache for cross-project reuse\n *\n * Structure:\n * ~/.skilld/references/<pkg>@<version>/sections/_BEST_PRACTICES.md\n */\nexport function writeSections(name: string, version: string, sections: Array<{ file: string, content: string }>): void {\n const cacheDir = getCacheDir(name, version)\n const sectionsDir = join(cacheDir, 'sections')\n mkdirSync(sectionsDir, { recursive: true, mode: 0o700 })\n for (const { file, content } of sections) {\n writeFileSync(join(sectionsDir, file), content, { mode: 0o600 })\n }\n}\n\n/**\n * Read a cached section from the global references dir\n */\nexport function readCachedSection(name: string, version: string, file: string): string | null {\n const path = join(getCacheDir(name, version), 'sections', file)\n if (!existsSync(path))\n return null\n return readFileSync(path, 'utf-8')\n}\n\n/**\n * Create symlink from skills dir to shipped skill dir\n */\nexport function linkShippedSkill(baseDir: string, skillName: string, targetDir: string): void {\n const linkPath = join(baseDir, skillName)\n if (existsSync(linkPath)) {\n const stat = lstatSync(linkPath)\n if (stat.isSymbolicLink())\n unlinkSync(linkPath)\n else rmSync(linkPath, { recursive: true, force: true })\n }\n symlinkSync(targetDir, linkPath)\n}\n\nexport function hasShippedDocs(name: string, cwd: string, version?: string): boolean {\n const pkgPath = resolvePkgDir(name, cwd, version)\n if (!pkgPath)\n return false\n\n const docsCandidates = ['docs', 'documentation', 'doc']\n for (const candidate of docsCandidates) {\n const docsPath = join(pkgPath, candidate)\n if (existsSync(docsPath))\n return true\n }\n return false\n}\n\n/**\n * List all cached packages\n */\nexport function listCached(): CachedPackage[] {\n if (!existsSync(REFERENCES_DIR))\n return []\n\n return readdirSync(REFERENCES_DIR)\n .filter(name => name.includes('@'))\n .map((dir) => {\n const [name, version] = dir.split('@')\n return { name: name!, version: version!, dir: join(REFERENCES_DIR, dir) }\n })\n}\n\n/**\n * Read cached docs for a package\n */\nexport function readCachedDocs(name: string, version: string): CachedDoc[] {\n const cacheDir = getCacheDir(name, version)\n if (!existsSync(cacheDir))\n return []\n\n const docs: CachedDoc[] = []\n\n function walk(dir: string, prefix = '') {\n for (const entry of readdirSync(dir, { withFileTypes: true })) {\n const entryPath = join(dir, entry.name)\n const relativePath = prefix ? `${prefix}/${entry.name}` : entry.name\n\n if (entry.isDirectory()) {\n walk(entryPath, relativePath)\n }\n else if (entry.name.endsWith('.md') || entry.name.endsWith('.mdx')) {\n docs.push({\n path: relativePath,\n content: readFileSync(entryPath, 'utf-8'),\n })\n }\n }\n }\n\n walk(cacheDir)\n return docs\n}\n\n/**\n * Clear cache for a specific package\n */\nexport function clearCache(name: string, version: string): boolean {\n const cacheDir = getCacheDir(name, version)\n if (!existsSync(cacheDir))\n return false\n\n rmSync(cacheDir, { recursive: true })\n return true\n}\n\n/**\n * Clear all cache\n */\nexport function clearAllCache(): number {\n const packages = listCached()\n for (const pkg of packages) {\n clearCache(pkg.name, pkg.version)\n }\n // Also clear repo-level cache\n if (existsSync(REPOS_DIR))\n rmSync(REPOS_DIR, { recursive: true })\n return packages.length\n}\n\n/**\n * List files in .skilld directory (pkg + docs) as relative paths for prompt context\n * Returns paths like ./.skilld/pkg/README.md, ./.skilld/docs/api.md\n */\nexport function listReferenceFiles(skillDir: string, maxDepth = 3): string[] {\n const referencesDir = join(skillDir, '.skilld')\n if (!existsSync(referencesDir))\n return []\n\n const files: string[] = []\n\n function walk(dir: string, depth: number) {\n if (depth > maxDepth)\n return\n try {\n for (const entry of readdirSync(dir, { withFileTypes: true })) {\n const full = join(dir, entry.name)\n if (entry.isDirectory() || entry.isSymbolicLink()) {\n try {\n const stat = statSync(full)\n if (stat.isDirectory()) {\n walk(full, depth + 1)\n continue\n }\n }\n catch { continue }\n }\n if (entry.name.endsWith('.md')) {\n files.push(full)\n }\n }\n }\n catch {\n // Broken symlink or permission error\n }\n }\n\n walk(referencesDir, 0)\n return files\n}\n"],"mappings":";;;;;AAYA,SAAS,YAAY,QAAgB,UAAwB;CAC3D,MAAM,WAAW,QAAQ,OAAO;AAChC,KAAI,CAAC,SAAS,WAAW,eAAe,IAAI,CAAC,SAAS,WAAW,UAAU,CACzE,OAAM,IAAI,MAAM,wCAAwC,WAAW;AAErE,KAAI;EACF,MAAM,OAAO,UAAU,SAAS;AAChC,MAAI,KAAK,gBAAgB,IAAI,KAAK,QAAQ,CACxC,YAAW,SAAS;SAElB;AACN,aAAY,QAAQ,UAAU,WAAW;;;;;AAM3C,SAAgB,SAAS,MAAc,SAA0B;AAC/D,QAAO,WAAW,YAAY,MAAM,QAAQ,CAAC;;;;;AAM/C,SAAgB,iBAAuB;AACrC,WAAU,gBAAgB;EAAE,WAAW;EAAM,MAAM;EAAO,CAAC;AAC3D,WAAU,WAAW;EAAE,WAAW;EAAM,MAAM;EAAO,CAAC;;;;;AAMxD,SAAgB,aACd,MACA,SACA,MACQ;CACR,MAAM,WAAW,YAAY,MAAM,QAAQ;AAC3C,WAAU,UAAU;EAAE,WAAW;EAAM,MAAM;EAAO,CAAC;AAErD,MAAK,MAAM,OAAO,MAAM;EACtB,MAAM,WAAW,KAAK,UAAU,IAAI,KAAK;AACzC,YAAU,KAAK,UAAU,KAAK,EAAE;GAAE,WAAW;GAAM,MAAM;GAAO,CAAC;AACjE,gBAAc,UAAU,iBAAiB,IAAI,QAAQ,EAAE,EAAE,MAAM,KAAO,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