@zaganjade/pi-multi-skill 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -0
  3. package/package.json +34 -0
  4. package/src/index.ts +427 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ZaganJade
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # pi-multi-skill
2
+
3
+ Load multiple skills at once in [pi](https://github.com/earendil-works/pi-mono) via the `/skills` command.
4
+
5
+ ## Why?
6
+
7
+ Pi's built-in `/skill:name` only loads one skill at a time. When you need multiple skills working together (e.g. `frontend-design` + `motion-design` + `test-driven-development`), you'd have to invoke them one by one. This extension lets you chain them in a single command.
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ /skills frontend-design,motion-design Create an animated landing page
13
+ /skills test-driven-development,systematic-debugging Fix the failing tests
14
+ /skills → show help + list available skills
15
+ ```
16
+
17
+ - **Comma-separated** skill names after `/skills`
18
+ - **Optional instructions** after the skill list (passed to the agent alongside the skill content)
19
+ - **Autocomplete** — typing `/skills ` shows all available skills with descriptions, and selecting one appends it with a comma for chaining
20
+
21
+ ### Legacy formats (also supported)
22
+
23
+ ```
24
+ /skills:frontend-design,motion-design [args] (colon + comma)
25
+ /skill:frontend-design+motion-design [args] (colon + plus)
26
+ ```
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ pi install git:github.com/ZaganJade/pi-multi-skill
32
+ ```
33
+
34
+ Or from a local path:
35
+
36
+ ```bash
37
+ pi install ./multi-skill
38
+ ```
39
+
40
+ ## How it works
41
+
42
+ The extension discovers all available skills via `pi.getCommands()` (covers user-level, project-level, and package-installed skills), reads each selected skill's `SKILL.md`, strips frontmatter, wraps it in `<skill>` blocks, and sends the combined content as a user message to trigger agent processing.
43
+
44
+ ## Files
45
+
46
+ | File | Purpose |
47
+ |------|---------|
48
+ | `src/index.ts` | Entry point — registers `/skills` command, autocomplete, and input handler |
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@zaganjade/pi-multi-skill",
3
+ "version": "1.0.0",
4
+ "description": "Load multiple skills at once via /skills command. Supports comma-separated skill names with autocomplete and instructions.",
5
+ "keywords": [
6
+ "pi-package"
7
+ ],
8
+ "license": "MIT",
9
+ "author": "ZaganJade <tipstrik81@gmail.com>",
10
+ "homepage": "https://github.com/ZaganJade/pi-extension/tree/main/multi-skill",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ZaganJade/pi-extension.git",
14
+ "directory": "multi-skill"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/ZaganJade/pi-extension/issues"
18
+ },
19
+ "main": "./src/index.ts",
20
+ "files": [
21
+ "src/",
22
+ "README.md",
23
+ "LICENSE"
24
+ ],
25
+ "pi": {
26
+ "extensions": [
27
+ "./src/index.ts"
28
+ ]
29
+ },
30
+ "peerDependencies": {
31
+ "@earendil-works/pi-coding-agent": "*",
32
+ "@earendil-works/pi-tui": "*"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,427 @@
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
+ import type { AutocompleteItem } from "@earendil-works/pi-tui";
6
+
7
+ /**
8
+ * Multi-Skill Loader Extension for Pi
9
+ *
10
+ * Enables loading multiple skills at once via the /skills command.
11
+ * Appears in the slash autocomplete menu alongside built-in commands,
12
+ * with full skill descriptions shown just like /skill: commands.
13
+ *
14
+ * Uses pi.getCommands() to discover ALL skills from every source:
15
+ * user-level, project-level, npm packages, git packages, etc.
16
+ *
17
+ * Usage:
18
+ * /skills frontend-design,motion-design Create an animated landing page
19
+ * /skills → shows help + available skills
20
+ *
21
+ * Also handles legacy formats via the input event:
22
+ * /skills:frontend-design,motion-design [args] (colon-separated)
23
+ * /skill:frontend-design+motion-design [args] (plus-separated)
24
+ */
25
+
26
+ // ─── Types ──────────────────────────────────────────────────────────────────
27
+
28
+ interface SkillInfo {
29
+ name: string;
30
+ description: string;
31
+ filePath: string;
32
+ baseDir: string;
33
+ }
34
+
35
+ // ─── Helpers ────────────────────────────────────────────────────────────────
36
+
37
+ /** Normalize CRLF → LF then strip YAML frontmatter, return body. */
38
+ function stripFrontmatter(content: string): string {
39
+ const normalized = content.replace(/\r\n/g, "\n");
40
+ const match = normalized.match(/^---\n([\s\S]*?\n)?---\n?/);
41
+ return match ? normalized.slice(match[0].length) : normalized;
42
+ }
43
+
44
+ /** Truncate a description to a single readable line for autocomplete display. */
45
+ function truncateDescription(desc: string, maxLen = 120): string {
46
+ if (!desc) return "";
47
+
48
+ // Take first non-empty line
49
+ const lines = desc
50
+ .split("\n")
51
+ .map((l) => l.replace(/^>\s*/, "").trim())
52
+ .filter((l) => l.length > 0);
53
+
54
+ if (lines.length === 0) return "";
55
+
56
+ let text = lines[0];
57
+ if (lines.length > 1 && text.length < 40) {
58
+ text = `${text} ${lines[1]}`;
59
+ }
60
+
61
+ if (text.length > maxLen) {
62
+ text = `${text.slice(0, maxLen - 1)}…`;
63
+ }
64
+
65
+ return text;
66
+ }
67
+
68
+ function getAgentDir(): string {
69
+ return join(homedir(), ".pi", "agent");
70
+ }
71
+
72
+ /**
73
+ * Discover ALL available skills via pi.getCommands().
74
+ * This covers user-level, project-level, and package-installed skills.
75
+ */
76
+ function discoverSkillsFromPi(pi: ExtensionAPI): SkillInfo[] {
77
+ const commands = pi.getCommands();
78
+ const skills: SkillInfo[] = [];
79
+
80
+ for (const cmd of commands) {
81
+ if (cmd.source !== "skill") continue;
82
+
83
+ // Command name is like "skill:name" → extract the skill name
84
+ const skillName = cmd.name.startsWith("skill:")
85
+ ? cmd.name.slice(6)
86
+ : cmd.name;
87
+
88
+ const filePath = cmd.sourceInfo.path;
89
+ const baseDir = cmd.sourceInfo.baseDir || dirname(filePath);
90
+
91
+ skills.push({
92
+ name: skillName,
93
+ description: truncateDescription(cmd.description || ""),
94
+ filePath,
95
+ baseDir,
96
+ });
97
+ }
98
+
99
+ return skills.sort((a, b) => a.name.localeCompare(b.name));
100
+ }
101
+
102
+ /**
103
+ * Fallback: scan filesystem for skills not yet discovered via pi.getCommands().
104
+ * Used to catch any edge cases.
105
+ */
106
+ function discoverSkillsFromFilesystem(
107
+ cwd: string,
108
+ knownNames: Set<string>,
109
+ ): SkillInfo[] {
110
+ const skills: SkillInfo[] = [];
111
+ const dirs = [
112
+ join(getAgentDir(), "skills"),
113
+ join(cwd, ".pi", "skills"),
114
+ ].filter((d) => existsSync(d));
115
+
116
+ for (const dir of dirs) {
117
+ try {
118
+ const entries = readdirSync(dir, { withFileTypes: true });
119
+ for (const entry of entries) {
120
+ if (entry.isDirectory()) {
121
+ const skillFile = join(dir, entry.name, "SKILL.md");
122
+ if (existsSync(skillFile) && !knownNames.has(entry.name)) {
123
+ skills.push({
124
+ name: entry.name,
125
+ description: "",
126
+ filePath: skillFile,
127
+ baseDir: join(dir, entry.name),
128
+ });
129
+ knownNames.add(entry.name);
130
+ }
131
+ } else if (entry.isFile() && entry.name.endsWith(".md")) {
132
+ const name = entry.name.replace(/\.md$/, "");
133
+ if (!knownNames.has(name)) {
134
+ const skillFile = join(dir, entry.name);
135
+ skills.push({
136
+ name,
137
+ description: "",
138
+ filePath: skillFile,
139
+ baseDir: dir,
140
+ });
141
+ knownNames.add(name);
142
+ }
143
+ }
144
+ }
145
+ } catch {
146
+ // Skip unreadable directories
147
+ }
148
+ }
149
+
150
+ return skills;
151
+ }
152
+
153
+ function resolveAndReadSkill(skillName: string, cwd: string): string | null {
154
+ // Try all possible skill locations
155
+ const dirs = [join(getAgentDir(), "skills"), join(cwd, ".pi", "skills")];
156
+
157
+ for (const dir of dirs) {
158
+ // Directory-based: <dir>/<name>/SKILL.md
159
+ const skillFile = join(dir, skillName, "SKILL.md");
160
+ try {
161
+ const content = readFileSync(skillFile, "utf-8");
162
+ const body = stripFrontmatter(content).trim();
163
+ return `<skill name="${skillName}" location="${skillFile}">\nReferences are relative to ${dirname(skillFile)}.\n\n${body}\n</skill>`;
164
+ } catch {
165
+ // not found
166
+ }
167
+
168
+ // File-based: <dir>/<name>.md
169
+ const mdFile = join(dir, `${skillName}.md`);
170
+ try {
171
+ const content = readFileSync(mdFile, "utf-8");
172
+ const body = stripFrontmatter(content).trim();
173
+ return `<skill name="${skillName}" location="${mdFile}">\nReferences are relative to ${dirname(mdFile)}.\n\n${body}\n</skill>`;
174
+ } catch {
175
+ // not found
176
+ }
177
+ }
178
+
179
+ return null;
180
+ }
181
+
182
+ /**
183
+ * Read a skill file by its absolute path (for package-installed skills
184
+ * that live outside ~/.pi/agent/skills/).
185
+ */
186
+ function resolveAndReadSkillByPath(
187
+ skillName: string,
188
+ filePath: string,
189
+ ): string | null {
190
+ try {
191
+ const content = readFileSync(filePath, "utf-8");
192
+ const body = stripFrontmatter(content).trim();
193
+ const baseDir = dirname(filePath);
194
+ return `<skill name="${skillName}" location="${filePath}">\nReferences are relative to ${baseDir}.\n\n${body}\n</skill>`;
195
+ } catch {
196
+ return null;
197
+ }
198
+ }
199
+
200
+ /** Build the combined skill block text from a list of skill infos. */
201
+ function buildCombinedSkills(
202
+ selectedSkills: SkillInfo[],
203
+ cwd: string,
204
+ instructions?: string,
205
+ ): string {
206
+ const expandedBlocks: string[] = [];
207
+ const notFound: string[] = [];
208
+
209
+ for (const skill of selectedSkills) {
210
+ // Try reading by the known filePath first (works for package skills)
211
+ let content = resolveAndReadSkillByPath(skill.name, skill.filePath);
212
+
213
+ // Fallback to filesystem scan for user/project skills
214
+ if (!content) {
215
+ content = resolveAndReadSkill(skill.name, cwd);
216
+ }
217
+
218
+ if (content) expandedBlocks.push(content);
219
+ else notFound.push(skill.name);
220
+ }
221
+
222
+ let combined = expandedBlocks.join("\n\n");
223
+
224
+ if (notFound.length > 0) {
225
+ combined += `\n\n> ⚠️ Skills not found: ${notFound.join(", ")}`;
226
+ }
227
+
228
+ if (instructions) {
229
+ combined += `\n\n${instructions}`;
230
+ }
231
+
232
+ return combined;
233
+ }
234
+
235
+ // ─── Extension ──────────────────────────────────────────────────────────────
236
+
237
+ export default function (pi: ExtensionAPI) {
238
+ // Cache skills list for the session (refreshed on reload)
239
+ let cachedSkills: SkillInfo[] | null = null;
240
+
241
+ function getSkills(cwd?: string): SkillInfo[] {
242
+ if (!cachedSkills) {
243
+ // Primary: use pi.getCommands() which covers ALL sources
244
+ const piSkills = discoverSkillsFromPi(pi);
245
+ const knownNames = new Set(piSkills.map((s) => s.name));
246
+
247
+ // Secondary: filesystem fallback for anything missed
248
+ const fsSkills = cwd ? discoverSkillsFromFilesystem(cwd, knownNames) : [];
249
+
250
+ cachedSkills = [...piSkills, ...fsSkills].sort((a, b) =>
251
+ a.name.localeCompare(b.name),
252
+ );
253
+ }
254
+ return cachedSkills;
255
+ }
256
+
257
+ // Clear cache on session start (skills might have changed)
258
+ pi.on("session_start", async () => {
259
+ cachedSkills = null;
260
+ });
261
+
262
+ // ========================================================================
263
+ // /skills command — registered as an extension command so it appears in
264
+ // the slash autocomplete menu when the user types "/".
265
+ // ========================================================================
266
+ pi.registerCommand("skills", {
267
+ description:
268
+ "Load multiple skills at once. Usage: /skills skill1,skill2 [instructions]",
269
+ getArgumentCompletions: (prefix: string): AutocompleteItem[] | null => {
270
+ const allSkills = getSkills();
271
+ if (allSkills.length === 0) return null;
272
+
273
+ // Parse comma-separated skills already typed
274
+ // e.g. prefix = "frontend-design,mot" → already = ["frontend-design"], current = "mot"
275
+ const parts = prefix.split(",");
276
+ const currentPart = parts[parts.length - 1].trim();
277
+ const alreadySelected = parts
278
+ .slice(0, -1)
279
+ .map((s) => s.trim())
280
+ .filter((s) => s.length > 0);
281
+
282
+ // Filter out already selected skills
283
+ const remaining = allSkills.filter(
284
+ (s) => !alreadySelected.includes(s.name),
285
+ );
286
+
287
+ let candidates = remaining;
288
+ if (currentPart) {
289
+ candidates = remaining.filter((s) => s.name.startsWith(currentPart));
290
+ }
291
+
292
+ if (candidates.length === 0) return null;
293
+
294
+ // If user has already typed some skills, show completion with comma prefix
295
+ // so selecting appends properly
296
+ const needsComma = alreadySelected.length > 0;
297
+ return candidates.map((s) => ({
298
+ value: needsComma
299
+ ? `${parts.slice(0, -1).join(",")},${s.name}`
300
+ : s.name,
301
+ label: alreadySelected.length > 0 ? ` ${s.name}` : s.name,
302
+ description: s.description || undefined,
303
+ }));
304
+ },
305
+ handler: async (args, ctx) => {
306
+ const available = getSkills(ctx.cwd);
307
+
308
+ if (!args || args.trim().length === 0) {
309
+ // No args → show help with available skills list + descriptions
310
+ const skillLines = available
311
+ .map((s) =>
312
+ s.description
313
+ ? ` • ${s.name} — ${s.description}`
314
+ : ` • ${s.name}`,
315
+ )
316
+ .join("\n");
317
+
318
+ ctx.ui.notify(
319
+ `Usage: /skills skill1,skill2,... [additional instructions]\n` +
320
+ `Example: /skills frontend-design,motion-design Create an animated page\n\n` +
321
+ `Available skills (${available.length}):\n${skillLines}`,
322
+ "info",
323
+ );
324
+ return;
325
+ }
326
+
327
+ // Parse: first token is comma-separated skill names, rest is instructions
328
+ const tokens = args.trim().match(/^(\S+)(?:\s+([\s\S]*))?$/);
329
+ if (!tokens) {
330
+ ctx.ui.notify(
331
+ "Invalid format. Usage: /skills skill1,skill2 [instructions]",
332
+ "error",
333
+ );
334
+ return;
335
+ }
336
+
337
+ const skillNames = tokens[1]
338
+ .split(",")
339
+ .map((s) => s.trim())
340
+ .filter((s) => s.length > 0);
341
+ const instructions = tokens[2]?.trim() || "";
342
+
343
+ if (skillNames.length === 0) {
344
+ ctx.ui.notify("No skill names provided.", "error");
345
+ return;
346
+ }
347
+
348
+ // Resolve skill infos from names
349
+ const nameToSkill = new Map(available.map((s) => [s.name, s]));
350
+ const selectedSkills = skillNames
351
+ .map((name) => nameToSkill.get(name))
352
+ .filter((s): s is SkillInfo => s !== undefined);
353
+
354
+ const combined = buildCombinedSkills(
355
+ selectedSkills.length > 0 ? selectedSkills : [],
356
+ ctx.cwd,
357
+ instructions || undefined,
358
+ );
359
+
360
+ // Check if any skills were actually loaded
361
+ if (!combined.includes("<skill ")) {
362
+ ctx.ui.notify(`No skills found for: ${skillNames.join(", ")}`, "error");
363
+ return;
364
+ }
365
+
366
+ // Send the combined skill content as a user message to trigger agent processing
367
+ pi.sendUserMessage(combined);
368
+ },
369
+ });
370
+
371
+ // ========================================================================
372
+ // Input event — handles legacy formats that use colons/plus signs.
373
+ // These formats bypass the command system, so we intercept via input.
374
+ //
375
+ // Supported:
376
+ // /skills:name1,name2,... [args] (colon + comma)
377
+ // /skill:name1+name2+... [args] (colon + plus)
378
+ // ========================================================================
379
+ pi.on("input", async (event, _ctx) => {
380
+ const text = event.text.trim();
381
+
382
+ // Only handle /skills:... or /skill:...+... formats
383
+ // /skills ... (space) is handled by the registered command above
384
+ if (!text.startsWith("/skills:") && !text.startsWith("/skill:"))
385
+ return { action: "continue" };
386
+
387
+ // For /skill:... with no plus sign, let pi's built-in handler deal with it
388
+ if (text.startsWith("/skill:") && !text.slice(7).includes("+")) {
389
+ return { action: "continue" };
390
+ }
391
+
392
+ // Parse the skill list
393
+ const colonIndex = text.indexOf(":");
394
+ const spaceIndex = text.indexOf(" ", colonIndex);
395
+ const skillListRaw =
396
+ spaceIndex === -1
397
+ ? text.slice(colonIndex + 1)
398
+ : text.slice(colonIndex + 1, spaceIndex);
399
+ const args = spaceIndex === -1 ? "" : text.slice(spaceIndex + 1).trim();
400
+
401
+ // Determine separator: comma for /skills:, plus for /skill:
402
+ const separator = text.startsWith("/skills:") ? "," : "+";
403
+ const skillNames = skillListRaw
404
+ .split(separator)
405
+ .map((s) => s.trim())
406
+ .filter((s) => s.length > 0);
407
+
408
+ if (skillNames.length === 0) return { action: "continue" };
409
+
410
+ // Resolve skills from the full skill list
411
+ const available = getSkills(_ctx.cwd);
412
+ const nameToSkill = new Map(available.map((s) => [s.name, s]));
413
+ const selectedSkills = skillNames
414
+ .map((name) => nameToSkill.get(name))
415
+ .filter((s): s is SkillInfo => s !== undefined);
416
+
417
+ const combined = buildCombinedSkills(
418
+ selectedSkills.length > 0 ? selectedSkills : [],
419
+ _ctx.cwd,
420
+ args || undefined,
421
+ );
422
+
423
+ if (!combined.includes("<skill ")) return { action: "continue" };
424
+
425
+ return { action: "transform", text: combined };
426
+ });
427
+ }