@henryqw/pi-add-dir 0.1.4 → 0.1.6

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.
@@ -1,4 +1,4 @@
1
- import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
1
+ import { closeSync, openSync, readdirSync, readFileSync, readSync, realpathSync, statSync } from "node:fs";
2
2
  import { readdir } from "node:fs/promises";
3
3
  import { homedir } from "node:os";
4
4
  import * as path from "node:path";
@@ -128,6 +128,29 @@ function skillDescription(content: string): string {
128
128
  return value.replace(/^("|')|("|')$/g, "").trim() || "No description";
129
129
  }
130
130
 
131
+ // ponytail: 8KB head is enough for SKILL.md frontmatter descriptions; full-body reads stay in scanDirContext
132
+ function readHead(filePath: string, bytes = 8192): string | null {
133
+ let fd: number | undefined;
134
+ try {
135
+ fd = openSync(filePath, "r");
136
+ const buffer = Buffer.alloc(bytes);
137
+ const bytesRead = readSync(fd, buffer, 0, bytes, 0);
138
+ return buffer.toString("utf8", 0, bytesRead);
139
+ } catch {
140
+ return null;
141
+ } finally {
142
+ if (fd !== undefined) closeSync(fd);
143
+ }
144
+ }
145
+
146
+ // ponytail: 8KB head covers typical frontmatter; falls back to full read only when opener present but closer missing (rare)
147
+ function readSkillFrontmatter(filePath: string): string | null {
148
+ const head = readHead(filePath, 8192);
149
+ if (head === null || !/^---\r?\n/.test(head)) return head;
150
+ if (/^---\r?\n[\s\S]*\r?\n---(?:\r?\n|$)/.test(head)) return head;
151
+ return readFileSafe(filePath);
152
+ }
153
+
131
154
  export function buildContextInjection(dirs: AddedDir[]): string {
132
155
  if (dirs.length === 0) return "";
133
156
 
@@ -138,23 +161,20 @@ export function buildContextInjection(dirs: AddedDir[]): string {
138
161
 
139
162
  const registeredSkills = new Set<string>();
140
163
  for (const dir of dirs) {
141
- const ctx = scanDirContext(dir.absolutePath);
142
- const skills = [...ctx.skills].filter(([name]) => {
143
- if (registeredSkills.has(name)) return false;
144
- registeredSkills.add(name);
145
- return true;
146
- });
147
164
  sections.push(`### ${dir.label} - \`${dir.absolutePath}\``);
148
165
 
149
- if (ctx.agentsMd) sections.push(`\n#### AGENTS.md (from ${dir.label})\n${ctx.agentsMd}`);
150
- if (ctx.claudeMd) sections.push(`\n#### CLAUDE.md (from ${dir.label})\n${ctx.claudeMd}`);
166
+ const agentsMd = readContextFile(dir.absolutePath, "AGENTS.md");
167
+ if (agentsMd) sections.push(`\n#### AGENTS.md (from ${dir.label})\n${agentsMd}`);
168
+ const claudeMd = readContextFile(dir.absolutePath, "CLAUDE.md");
169
+ if (claudeMd) sections.push(`\n#### CLAUDE.md (from ${dir.label})\n${claudeMd}`);
151
170
 
171
+ const skills = skillFiles(dir.absolutePath).filter((skill) => !registeredSkills.has(skill.name));
152
172
  if (skills.length > 0) {
153
173
  sections.push(`\n#### Skills from ${dir.label} (registered as /skill:name commands):`);
154
- for (const [name, content] of skills) {
155
- sections.push(
156
- `- **${name}**: ${skillDescription(content)} - use \`/skill:${name}\` or read \`${ctx.skillPaths.get(name)}\``,
157
- );
174
+ for (const skill of skills) {
175
+ registeredSkills.add(skill.name);
176
+ const description = skillDescription(readSkillFrontmatter(skill.path) ?? "");
177
+ sections.push(`- **${skill.name}**: ${description} - use \`/skill:${skill.name}\` or read \`${skill.path}\``);
158
178
  }
159
179
  }
160
180
  }
@@ -170,6 +170,16 @@ export default function addDirExtension(pi: ExtensionAPI): void {
170
170
  hasNewSkills: false,
171
171
  };
172
172
  }
173
+ const overlap = addedDirs.find(
174
+ (dir) => isWithinDir(dir.absolutePath, absolutePath) || isWithinDir(absolutePath, dir.absolutePath),
175
+ );
176
+ if (overlap) {
177
+ return {
178
+ ok: false,
179
+ message: `Directory overlaps already-added directory: ${overlap.absolutePath}`,
180
+ hasNewSkills: false,
181
+ };
182
+ }
173
183
 
174
184
  const context = scanDirContext(absolutePath);
175
185
  const label = basename(absolutePath) || absolutePath;
@@ -382,7 +392,6 @@ export default function addDirExtension(pi: ExtensionAPI): void {
382
392
  renderCall(args, theme) {
383
393
  let text = theme.fg("toolTitle", theme.bold("search_external_files "));
384
394
  text += theme.fg("accent", `"${cleanPath(args.pattern)}"`);
385
- text += theme.fg("dim", ` across ${addedDirs.length} dir(s)`);
386
395
  return new Text(text, 0, 0);
387
396
  },
388
397
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henryqw/pi-add-dir",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Add external directories to a Pi session with context, skills, and file search.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "scripts": {
23
23
  "test": "node --test test/*.test.ts",
24
- "typecheck": "tsc --noEmit --allowImportingTsExtensions --target ES2022 --module NodeNext --moduleResolution NodeNext --skipLibCheck extensions/*.ts test/*.test.ts",
24
+ "typecheck": "tsc --noEmit --allowImportingTsExtensions --target ES2022 --module NodeNext --moduleResolution NodeNext --skipLibCheck extensions/*.ts",
25
25
  "pack:check": "npm pack --dry-run"
26
26
  },
27
27
  "peerDependencies": {