@xynogen/pix-skills 0.5.4 → 0.6.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 +96 -4
- package/package.json +2 -1
- package/skills/subagent.md +63 -6
- package/src/directive.ts +1 -6
- package/src/index.test.ts +276 -27
- package/src/index.ts +270 -45
- package/src/run.test.ts +1 -3
- package/src/run.ts +2 -5
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Pi coding agent extension — skill loader tool + skills bundle.
|
|
|
6
6
|
|
|
7
7
|
| Resource | Type | Description |
|
|
8
8
|
|---|---|---|
|
|
9
|
-
| `read_skills` | tool | Browse and
|
|
9
|
+
| `read_skills` | tool | Browse skills, read references, and copy bundled resources. No args → list all. `name` only → description. `name + full=true` → instructions. `name + resource` → read reference. Add `output` → copy raw file into project. |
|
|
10
10
|
| `skills/` | skills | 27 bundled skills (off-context by default — discovered on demand via `read_skills`) |
|
|
11
11
|
|
|
12
12
|
## How it works
|
|
@@ -17,8 +17,11 @@ the baseline context small regardless of how many skills ship.
|
|
|
17
17
|
|
|
18
18
|
Discovery and loading go through the `read_skills` tool instead:
|
|
19
19
|
|
|
20
|
-
- `read_skills()` — lists
|
|
20
|
+
- `read_skills()` — lists skill names only in one horizontal line (independent dir scan, not the prompt registry)
|
|
21
|
+
- `read_skills(name=<skill>)` — reads one skill's description
|
|
21
22
|
- `read_skills(name=<skill>, full=true)` — loads the full procedure into context
|
|
23
|
+
- `read_skills(name=<skill>, resource=references/<path>)` — reads a UTF-8 reference into context
|
|
24
|
+
- `read_skills(name=<skill>, resource=<path>, output=<path>)` — copies a reference, script, or asset into the current project
|
|
22
25
|
|
|
23
26
|
A skill can opt back into passive prompt injection by **removing** the `disable-model-invocation: true`
|
|
24
27
|
line (or setting it to `false`) — then pi auto-loads its description on description match. The bundled
|
|
@@ -26,6 +29,80 @@ set deliberately leaves it on to favour a lean context.
|
|
|
26
29
|
|
|
27
30
|
Skills are also discovered from `~/.pi/agent/skills/` (user-level). Bundled skills take precedence on name collision.
|
|
28
31
|
|
|
32
|
+
## Skill layout and bundled resources
|
|
33
|
+
|
|
34
|
+
`pix-skills` recognizes both the current flat layout and a directory bundle:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
skills/
|
|
38
|
+
├── debug.md # current flat layout
|
|
39
|
+
└── debug/
|
|
40
|
+
└── SKILL.md # equivalent minimal bundle layout
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
A bundle needs only `SKILL.md`. It may add resource directories when the skill
|
|
44
|
+
actually needs them:
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
skills/docx/
|
|
48
|
+
├── SKILL.md # required
|
|
49
|
+
├── scripts/ # optional executable source files
|
|
50
|
+
├── references/ # optional text/documentation resources
|
|
51
|
+
└── assets/ # optional data, templates, images, or themes
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The bundle relies exclusively on this conventional folder structure—there is
|
|
55
|
+
no separate metadata file. `SKILL.md` frontmatter is the single source of truth
|
|
56
|
+
for skill metadata, while the presence and contents of `scripts/`, `references/`,
|
|
57
|
+
and `assets/` define the available resources. Empty resource directories do not
|
|
58
|
+
need to be created.
|
|
59
|
+
|
|
60
|
+
### Resource access
|
|
61
|
+
|
|
62
|
+
References may be read directly into model context or copied into the current
|
|
63
|
+
project. Scripts and assets are never returned to context; they require an
|
|
64
|
+
explicit project-relative `output` and are copied as raw bytes:
|
|
65
|
+
|
|
66
|
+
```text
|
|
67
|
+
# Read a small reference into context (1 MiB text limit)
|
|
68
|
+
read_skills(name="docx", resource="references/compatibility.md")
|
|
69
|
+
|
|
70
|
+
# Copy a large reference without adding it to context
|
|
71
|
+
read_skills(
|
|
72
|
+
name="docx",
|
|
73
|
+
resource="references/specification.pdf",
|
|
74
|
+
output=".pi/resources/docx/specification.pdf"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# Materialize scripts and binary assets before using them
|
|
78
|
+
read_skills(
|
|
79
|
+
name="docx",
|
|
80
|
+
resource="scripts/render.ts",
|
|
81
|
+
output=".pi/tools/docx/render.ts"
|
|
82
|
+
)
|
|
83
|
+
read_skills(
|
|
84
|
+
name="docx",
|
|
85
|
+
resource="assets/template.docx",
|
|
86
|
+
output=".pi/tools/docx/template.docx"
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Copying does not execute a script. It creates missing destination directories
|
|
91
|
+
and atomically replaces the destination with the bundled file. The agent can
|
|
92
|
+
then use ordinary tools to inspect or run the copied resource.
|
|
93
|
+
|
|
94
|
+
Resource resolution will be confined to the selected skill bundle:
|
|
95
|
+
|
|
96
|
+
- only `scripts/`, `references/`, and `assets/` are addressable;
|
|
97
|
+
- absolute paths and `..` traversal are rejected;
|
|
98
|
+
- symlinks that resolve outside the canonical skill root are rejected;
|
|
99
|
+
- flat skills such as `skills/debug.md` have no resource root and therefore
|
|
100
|
+
cannot read neighboring skills or files;
|
|
101
|
+
- callers use bundle-relative names and never need an absolute `${SKILL_DIR}`;
|
|
102
|
+
- `output` is relative to the current project working directory;
|
|
103
|
+
- invalid source or output paths—including paths outside the permitted roots,
|
|
104
|
+
absolute paths, traversal, and escaping symlinks—are rejected.
|
|
105
|
+
|
|
29
106
|
`read_skills` is the safe version of "agent prompts itself":
|
|
30
107
|
|
|
31
108
|
- Agent calls tool explicitly — no autonomous injection
|
|
@@ -100,8 +177,13 @@ Directive commands are gated by the **same rule engine** as the `bash` tool
|
|
|
100
177
|
- **Single source of truth.** Expanding pix-gate's rule table or a user's
|
|
101
178
|
`~/.pi/agent/pix-gate.json` automatically tightens the skill path too.
|
|
102
179
|
|
|
103
|
-
Interpolation only happens on the `full=true` path; listing and description
|
|
104
|
-
lookups never run commands.
|
|
180
|
+
Interpolation only happens on the `full=true` path; name listing and description
|
|
181
|
+
lookups never run commands. Calls and results identify the operation as `list`,
|
|
182
|
+
`description`, `instructions`, `reference`, or `copy`; instruction results preview
|
|
183
|
+
at most the first 100 characters, while the full content still reaches the agent.
|
|
184
|
+
Copied-resource results show
|
|
185
|
+
the destination and byte size. Tool results follow Pix's configured auto-collapse
|
|
186
|
+
delay, including `read_skills`.
|
|
105
187
|
|
|
106
188
|
## Usage
|
|
107
189
|
|
|
@@ -114,6 +196,16 @@ read_skills(name="commit")
|
|
|
114
196
|
|
|
115
197
|
# Agent loads full commit procedure
|
|
116
198
|
read_skills(name="commit", full=true)
|
|
199
|
+
|
|
200
|
+
# Read a UTF-8 reference into context
|
|
201
|
+
read_skills(name="docx", resource="references/compatibility.md")
|
|
202
|
+
|
|
203
|
+
# Copy a script or asset to the current project without loading it into context
|
|
204
|
+
read_skills(
|
|
205
|
+
name="docx",
|
|
206
|
+
resource="scripts/render.ts",
|
|
207
|
+
output=".pi/tools/docx/render.ts"
|
|
208
|
+
)
|
|
117
209
|
```
|
|
118
210
|
|
|
119
211
|
## Install
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-skills",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Pi extension — agent skill loader (skill tool + skills bundle)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"@earendil-works/pi-tui": "*"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
+
"@xynogen/pix-data": "^0.3.2",
|
|
49
50
|
"@xynogen/pix-gate": "^0.1.13"
|
|
50
51
|
}
|
|
51
52
|
}
|
package/skills/subagent.md
CHANGED
|
@@ -59,13 +59,13 @@ Do **not** use when:
|
|
|
59
59
|
For simple/medium tasks, plan inline using the `plan` skill's discipline: fix the
|
|
60
60
|
exact file paths, the shared contracts (types, function signatures, interfaces),
|
|
61
61
|
the naming, and the acceptance criteria **before** splitting. The whole point is
|
|
62
|
-
that workers
|
|
62
|
+
that workers receive explicitly stated decisions instead of inventing them.
|
|
63
63
|
|
|
64
64
|
For large/complex tasks, delegate planning to the builtin `Plan` agent first, get
|
|
65
65
|
the plan, then decompose it:
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
|
-
subagent({ subagent_type: "Plan", prompt: "Plan: <task>. Emphasize independent, parallelizable units." })
|
|
68
|
+
subagent({ subagent_type: "Plan", thinking: "high", prompt: "Plan: <task>. Emphasize independent, parallelizable units." })
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
### 2. Decompose
|
|
@@ -135,6 +135,49 @@ whatever is current. So before fanning out:
|
|
|
135
135
|
3. If no clearly-cheaper option is obvious, **ask** which model to delegate to
|
|
136
136
|
rather than guessing — the wrong pick wastes the run.
|
|
137
137
|
|
|
138
|
+
#### Thinking budget: medium or high by default
|
|
139
|
+
|
|
140
|
+
Set the worker's `thinking` level explicitly on every subagent launch. Use only:
|
|
141
|
+
|
|
142
|
+
- **`medium`** for routine, bounded, or mechanical work; this is the default.
|
|
143
|
+
- **`high`** for genuinely complex analysis where deeper reasoning has a clear
|
|
144
|
+
expected benefit.
|
|
145
|
+
|
|
146
|
+
Treat **`high` as the maximum without user approval**. Do not use `off`,
|
|
147
|
+
`minimal`, `low`, or any level above `high` by default. A level above `high`
|
|
148
|
+
(such as `xhigh`) is allowed only when all of the following are true:
|
|
149
|
+
|
|
150
|
+
1. the task presents an exceptional, concrete need that `high` is unlikely to
|
|
151
|
+
handle adequately;
|
|
152
|
+
2. before launching the worker, explain to the user why the extra reasoning is
|
|
153
|
+
warranted, what useful outcome it is intended to improve, and the likely
|
|
154
|
+
latency or cost trade-off; and
|
|
155
|
+
3. explicitly ask for and receive the user's approval.
|
|
156
|
+
|
|
157
|
+
Model prestige, vague hopes of a "better" answer, or reviewer thoroughness alone
|
|
158
|
+
are not sufficient justification. If approval is absent or unclear, use
|
|
159
|
+
`high`. Never silently escalate a subagent above `high`.
|
|
160
|
+
|
|
161
|
+
#### Do not fork or inherit parent context
|
|
162
|
+
|
|
163
|
+
Avoid context forking entirely. Do not use `inherit_context: true`,
|
|
164
|
+
`prompt_mode: append`, or a custom agent configured to inherit the parent
|
|
165
|
+
conversation or system prompt. Forking repeatedly sends a large amount of
|
|
166
|
+
irrelevant parent context to every worker, burns input tokens, increases latency,
|
|
167
|
+
and can distract the worker with unrelated decisions.
|
|
168
|
+
|
|
169
|
+
Use the default replacement/isolated behavior and provide a compact,
|
|
170
|
+
self-contained task prompt containing only the goal, required contract,
|
|
171
|
+
constraints, relevant paths or excerpts, validation command, and expected
|
|
172
|
+
output. If a worker needs a fact from the parent conversation, copy only that
|
|
173
|
+
specific fact into its prompt; never pass the whole conversation for
|
|
174
|
+
convenience. If the task cannot be specified without inheriting the parent
|
|
175
|
+
context, keep it on the orchestrator instead of launching a subagent.
|
|
176
|
+
|
|
177
|
+
Before using a custom agent, check that its frontmatter does not enable
|
|
178
|
+
`inherit_context` or `prompt_mode: append`. Treat either setting as incompatible
|
|
179
|
+
with this delegation guide.
|
|
180
|
+
|
|
138
181
|
The per-task `model` override is the right mechanism precisely because the main
|
|
139
182
|
model can change: the override pins each worker to the chosen model regardless
|
|
140
183
|
of what the orchestrator is running at that moment. Examples below use
|
|
@@ -144,9 +187,9 @@ only when you deliberately want the worker to inherit the parent's model.
|
|
|
144
187
|
```typescript
|
|
145
188
|
subagent({
|
|
146
189
|
tasks: [
|
|
147
|
-
{ agent: "worker", model: "<cheap-model>", task: "<full self-contained spec for unit A>", output: "units/a.md", outputMode: "file-only" },
|
|
148
|
-
{ agent: "worker", model: "<cheap-model>", task: "<full self-contained spec for unit B>", output: "units/b.md", outputMode: "file-only" },
|
|
149
|
-
{ agent: "worker", model: "<cheap-model>", task: "<full self-contained spec for unit C>", output: "units/c.md", outputMode: "file-only" }
|
|
190
|
+
{ agent: "worker", model: "<cheap-model>", thinking: "medium", task: "<full self-contained spec for unit A>", output: "units/a.md", outputMode: "file-only" },
|
|
191
|
+
{ agent: "worker", model: "<cheap-model>", thinking: "medium", task: "<full self-contained spec for unit B>", output: "units/b.md", outputMode: "file-only" },
|
|
192
|
+
{ agent: "worker", model: "<cheap-model>", thinking: "medium", task: "<full self-contained spec for unit C>", output: "units/c.md", outputMode: "file-only" }
|
|
150
193
|
],
|
|
151
194
|
concurrency: 3,
|
|
152
195
|
async: true
|
|
@@ -168,6 +211,17 @@ Rules for the fan-out:
|
|
|
168
211
|
concurrently, isolate each with `worktree: true` instead of letting them
|
|
169
212
|
collide.
|
|
170
213
|
|
|
214
|
+
#### Supervise running workers
|
|
215
|
+
|
|
216
|
+
Background workers are not fire-and-forget. Estimate a reasonable duration from
|
|
217
|
+
the task size. If a worker runs materially longer than expected or available
|
|
218
|
+
progress shows it is blocked or off-track, inspect once and proactively use
|
|
219
|
+
`agent_steer` with concrete help: missing context, a corrected path or command,
|
|
220
|
+
or a narrower goal. Do not repeatedly poll, sleep-wait, or steer merely because
|
|
221
|
+
a healthy task is slow. If steering does not recover it, stop and relaunch only
|
|
222
|
+
with a better prompt. Use judgment; every steer should address an observed
|
|
223
|
+
problem.
|
|
224
|
+
|
|
171
225
|
#### Cheap-worker prompt contract
|
|
172
226
|
|
|
173
227
|
Because the worker is less capable, the spec must be **tighter**, not looser.
|
|
@@ -187,6 +241,7 @@ self-check before reporting done:
|
|
|
187
241
|
{
|
|
188
242
|
agent: "worker",
|
|
189
243
|
model: "<cheap-model>",
|
|
244
|
+
thinking: "medium",
|
|
190
245
|
task: "<full spec>",
|
|
191
246
|
acceptance: {
|
|
192
247
|
criteria: ["Conforms to the given contract", "Edits only the named files", "Local check passes"],
|
|
@@ -310,8 +365,10 @@ type's model, that's a bug in the precedence, not a feature of the type.
|
|
|
310
365
|
decompose, or spawn subagents.
|
|
311
366
|
- **Single writer per target.** Independent files or isolated worktrees only —
|
|
312
367
|
never concurrent writes to the same region.
|
|
368
|
+
- **Never fork context.** Do not enable `inherit_context` or `prompt_mode:
|
|
369
|
+
append`; pass only a compact, self-contained task prompt.
|
|
313
370
|
- **Self-contained specs.** Children don't inherit parent history; copy every
|
|
314
|
-
contract detail into the task string.
|
|
371
|
+
necessary contract detail into the task string.
|
|
315
372
|
- **Strong model integrates and verifies.** Cheap models do bulk units; the
|
|
316
373
|
orchestrator owns the seams and the final gate.
|
|
317
374
|
- **Resolve the worker model at call time.** The active model can change
|
package/src/directive.ts
CHANGED
|
@@ -37,12 +37,7 @@ export function findCommandDirectives(content: string): CommandDirective[] {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/** Replace the [start, end) slice of `s` with `text`. */
|
|
40
|
-
export function replaceSpan(
|
|
41
|
-
s: string,
|
|
42
|
-
start: number,
|
|
43
|
-
end: number,
|
|
44
|
-
text: string,
|
|
45
|
-
): string {
|
|
40
|
+
export function replaceSpan(s: string, start: number, end: number, text: string): string {
|
|
46
41
|
return s.slice(0, start) + text + s.slice(end);
|
|
47
42
|
}
|
|
48
43
|
|
package/src/index.test.ts
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import { describe, expect, it } from "bun:test";
|
|
1
|
+
import { afterEach, describe, expect, it } from "bun:test";
|
|
2
|
+
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
2
5
|
|
|
3
6
|
import {
|
|
7
|
+
copySkillResource,
|
|
4
8
|
extractDescription,
|
|
5
9
|
extractName,
|
|
6
10
|
findCommandDirectives,
|
|
11
|
+
formatCollapsedSkillResult,
|
|
12
|
+
formatExpandedSkillResult,
|
|
13
|
+
formatSkillCallLabel,
|
|
14
|
+
formatSkillList,
|
|
7
15
|
formatSkillSummary,
|
|
8
16
|
hasShellMeta,
|
|
9
17
|
interpolateSkill,
|
|
18
|
+
readSkillResource,
|
|
10
19
|
replaceSpan,
|
|
11
20
|
type ThemeLike,
|
|
12
21
|
tokenizeCommand,
|
|
@@ -46,9 +55,7 @@ describe("extractName", () => {
|
|
|
46
55
|
|
|
47
56
|
describe("extractDescription", () => {
|
|
48
57
|
it("reads description from frontmatter", () => {
|
|
49
|
-
expect(extractDescription(FRONTMATTER)).toBe(
|
|
50
|
-
"Split, write, and maintain commits.",
|
|
51
|
-
);
|
|
58
|
+
expect(extractDescription(FRONTMATTER)).toBe("Split, write, and maintain commits.");
|
|
52
59
|
});
|
|
53
60
|
|
|
54
61
|
it("returns null when absent", () => {
|
|
@@ -60,6 +67,100 @@ describe("extractDescription", () => {
|
|
|
60
67
|
});
|
|
61
68
|
});
|
|
62
69
|
|
|
70
|
+
describe("formatSkillList", () => {
|
|
71
|
+
it("lists skill names horizontally without descriptions", () => {
|
|
72
|
+
expect(formatSkillList(["commit", "debug"])).toBe("Available skills (2): commit · debug");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("formatSkillCallLabel", () => {
|
|
77
|
+
it("labels each operation distinctly", () => {
|
|
78
|
+
expect(formatSkillCallLabel({})).toBe("list");
|
|
79
|
+
expect(formatSkillCallLabel({ name: "test" })).toBe("description · test");
|
|
80
|
+
expect(formatSkillCallLabel({ name: "test", full: true })).toBe("instructions · test");
|
|
81
|
+
expect(formatSkillCallLabel({ name: "docx", resource: "references/guide.md" })).toBe(
|
|
82
|
+
"reference · docx/references/guide.md",
|
|
83
|
+
);
|
|
84
|
+
expect(
|
|
85
|
+
formatSkillCallLabel({
|
|
86
|
+
name: "docx",
|
|
87
|
+
resource: "scripts/render.ts",
|
|
88
|
+
output: ".pi/tools/render.ts",
|
|
89
|
+
}),
|
|
90
|
+
).toBe("copy · docx/scripts/render.ts → .pi/tools/render.ts");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("formatCollapsedSkillResult", () => {
|
|
95
|
+
it("summarizes each successful result distinctly", () => {
|
|
96
|
+
expect(formatCollapsedSkillResult({ mode: "list", count: 28 })).toBe("28 skills");
|
|
97
|
+
expect(formatCollapsedSkillResult({ mode: "description", name: "test" })).toBe(
|
|
98
|
+
"test · description",
|
|
99
|
+
);
|
|
100
|
+
expect(formatCollapsedSkillResult({ mode: "instructions", name: "test", lines: 42 })).toBe(
|
|
101
|
+
"test · 42 instruction lines",
|
|
102
|
+
);
|
|
103
|
+
expect(
|
|
104
|
+
formatCollapsedSkillResult({
|
|
105
|
+
mode: "reference",
|
|
106
|
+
name: "docx",
|
|
107
|
+
resource: "references/guide.md",
|
|
108
|
+
bytes: 120,
|
|
109
|
+
}),
|
|
110
|
+
).toBe("docx · reference · 120 B");
|
|
111
|
+
expect(
|
|
112
|
+
formatCollapsedSkillResult({
|
|
113
|
+
mode: "copy",
|
|
114
|
+
name: "docx",
|
|
115
|
+
resource: "scripts/render.ts",
|
|
116
|
+
output: ".pi/tools/render.ts",
|
|
117
|
+
bytes: 2048,
|
|
118
|
+
}),
|
|
119
|
+
).toBe("copied · .pi/tools/render.ts · 2.0 KiB");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("formatExpandedSkillResult", () => {
|
|
124
|
+
it("gives description, instructions, reference, and copy distinct output", () => {
|
|
125
|
+
expect(
|
|
126
|
+
formatExpandedSkillResult({ mode: "description", name: "test" }, "test: Test helper"),
|
|
127
|
+
).toBe("DESCRIPTION · test\nTest helper");
|
|
128
|
+
const instructions = "a".repeat(101);
|
|
129
|
+
expect(
|
|
130
|
+
formatExpandedSkillResult({ mode: "instructions", name: "test", lines: 42 }, instructions),
|
|
131
|
+
).toBe(`INSTRUCTIONS · test · 42 lines\n${"a".repeat(100)}...`);
|
|
132
|
+
expect(
|
|
133
|
+
formatExpandedSkillResult(
|
|
134
|
+
{ mode: "instructions", name: "test", lines: 1 },
|
|
135
|
+
"short instructions",
|
|
136
|
+
),
|
|
137
|
+
).toBe("INSTRUCTIONS · test · 1 lines\nshort instructions");
|
|
138
|
+
expect(
|
|
139
|
+
formatExpandedSkillResult(
|
|
140
|
+
{
|
|
141
|
+
mode: "reference",
|
|
142
|
+
name: "docx",
|
|
143
|
+
resource: "references/guide.md",
|
|
144
|
+
bytes: 120,
|
|
145
|
+
},
|
|
146
|
+
"portable guidance",
|
|
147
|
+
),
|
|
148
|
+
).toBe("REFERENCE · docx\nreferences/guide.md · 120 B\nportable guidance");
|
|
149
|
+
expect(
|
|
150
|
+
formatExpandedSkillResult(
|
|
151
|
+
{
|
|
152
|
+
mode: "copy",
|
|
153
|
+
name: "docx",
|
|
154
|
+
resource: "scripts/render.ts",
|
|
155
|
+
output: ".pi/tools/render.ts",
|
|
156
|
+
bytes: 2048,
|
|
157
|
+
},
|
|
158
|
+
"",
|
|
159
|
+
),
|
|
160
|
+
).toBe("COPIED · docx\nscripts/render.ts → .pi/tools/render.ts · 2.0 KiB");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
63
164
|
describe("formatSkillSummary", () => {
|
|
64
165
|
it("renders muted placeholder for empty input", () => {
|
|
65
166
|
expect(formatSkillSummary("", tagTheme)).toBe("[muted]No skills found.[/]");
|
|
@@ -67,9 +168,7 @@ describe("formatSkillSummary", () => {
|
|
|
67
168
|
|
|
68
169
|
it("renders single skill as bold-accent name + muted description", () => {
|
|
69
170
|
const out = formatSkillSummary(FRONTMATTER, tagTheme);
|
|
70
|
-
expect(out).toBe(
|
|
71
|
-
"[accent]<b>commit</b>[/] [muted]Split, write, and maintain commits.[/]",
|
|
72
|
-
);
|
|
171
|
+
expect(out).toBe("[accent]<b>commit</b>[/] [muted]Split, write, and maintain commits.[/]");
|
|
73
172
|
});
|
|
74
173
|
|
|
75
174
|
it("falls back to (no description) when frontmatter has name only", () => {
|
|
@@ -83,9 +182,7 @@ describe("formatSkillSummary", () => {
|
|
|
83
182
|
const lines = out.split("\n");
|
|
84
183
|
expect(lines).toHaveLength(2);
|
|
85
184
|
expect(lines[0]).toBe("[accent]<b>commit</b>[/] [muted]write commits[/]");
|
|
86
|
-
expect(lines[1]).toBe(
|
|
87
|
-
"[accent]<b>debug</b>[/] [muted]root cause analysis[/]",
|
|
88
|
-
);
|
|
185
|
+
expect(lines[1]).toBe("[accent]<b>debug</b>[/] [muted]root cause analysis[/]");
|
|
89
186
|
});
|
|
90
187
|
|
|
91
188
|
it("passes non 'name: desc' lines through as muted", () => {
|
|
@@ -94,6 +191,170 @@ describe("formatSkillSummary", () => {
|
|
|
94
191
|
});
|
|
95
192
|
});
|
|
96
193
|
|
|
194
|
+
describe("readSkillResource", () => {
|
|
195
|
+
const roots: string[] = [];
|
|
196
|
+
const makeBundle = async () => {
|
|
197
|
+
const root = await mkdtemp(join(tmpdir(), "pix-skill-resource-"));
|
|
198
|
+
roots.push(root);
|
|
199
|
+
await mkdir(join(root, "references"));
|
|
200
|
+
await mkdir(join(root, "scripts"));
|
|
201
|
+
await mkdir(join(root, "assets"));
|
|
202
|
+
return root;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
afterEach(async () => {
|
|
206
|
+
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("reads a file from a conventional resource directory", async () => {
|
|
210
|
+
const root = await makeBundle();
|
|
211
|
+
await writeFile(join(root, "references", "guide.md"), "portable guidance");
|
|
212
|
+
|
|
213
|
+
await expect(readSkillResource(root, "references/guide.md")).resolves.toBe("portable guidance");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("requires output instead of returning scripts or assets to context", async () => {
|
|
217
|
+
const root = await makeBundle();
|
|
218
|
+
await writeFile(join(root, "scripts", "render.ts"), "content");
|
|
219
|
+
await writeFile(join(root, "assets", "theme.json"), "{}");
|
|
220
|
+
|
|
221
|
+
await expect(readSkillResource(root, "scripts/render.ts")).rejects.toThrow(
|
|
222
|
+
"Output is required for scripts/ and assets/ resources",
|
|
223
|
+
);
|
|
224
|
+
await expect(readSkillResource(root, "assets/theme.json")).rejects.toThrow(
|
|
225
|
+
"Output is required for scripts/ and assets/ resources",
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("rejects files outside scripts, references, and assets", async () => {
|
|
230
|
+
const root = await makeBundle();
|
|
231
|
+
await writeFile(join(root, "private.txt"), "secret");
|
|
232
|
+
|
|
233
|
+
await expect(readSkillResource(root, "private.txt")).rejects.toThrow("Invalid resource path");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("rejects absolute paths and parent traversal", async () => {
|
|
237
|
+
const root = await makeBundle();
|
|
238
|
+
|
|
239
|
+
await expect(readSkillResource(root, "/etc/passwd")).rejects.toThrow("Invalid resource path");
|
|
240
|
+
await expect(readSkillResource(root, "references/../../secret")).rejects.toThrow(
|
|
241
|
+
"Invalid resource path",
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("rejects symlinks that escape the skill bundle", async () => {
|
|
246
|
+
const root = await makeBundle();
|
|
247
|
+
const outside = await mkdtemp(join(tmpdir(), "pix-skill-outside-"));
|
|
248
|
+
roots.push(outside);
|
|
249
|
+
await writeFile(join(outside, "secret.txt"), "secret");
|
|
250
|
+
await symlink(join(outside, "secret.txt"), join(root, "assets", "escape.txt"));
|
|
251
|
+
|
|
252
|
+
await expect(readSkillResource(root, "assets/escape.txt")).rejects.toThrow(
|
|
253
|
+
"Invalid resource path",
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe("copySkillResource", () => {
|
|
259
|
+
const roots: string[] = [];
|
|
260
|
+
const makeDirectories = async () => {
|
|
261
|
+
const bundle = await mkdtemp(join(tmpdir(), "pix-skill-copy-source-"));
|
|
262
|
+
const project = await mkdtemp(join(tmpdir(), "pix-skill-copy-output-"));
|
|
263
|
+
roots.push(bundle, project);
|
|
264
|
+
await mkdir(join(bundle, "scripts"));
|
|
265
|
+
await mkdir(join(bundle, "references"));
|
|
266
|
+
await mkdir(join(bundle, "assets"));
|
|
267
|
+
return { bundle, project };
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
afterEach(async () => {
|
|
271
|
+
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("copies a script as raw bytes into the project", async () => {
|
|
275
|
+
const { bundle, project } = await makeDirectories();
|
|
276
|
+
await writeFile(join(bundle, "scripts", "render.ts"), "console.log('render')\n");
|
|
277
|
+
|
|
278
|
+
const result = await copySkillResource(
|
|
279
|
+
bundle,
|
|
280
|
+
"scripts/render.ts",
|
|
281
|
+
project,
|
|
282
|
+
".pi/tools/render.ts",
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
expect(result.path).toBe(join(project, ".pi/tools/render.ts"));
|
|
286
|
+
expect(result.bytes).toBe(22);
|
|
287
|
+
await expect(Bun.file(result.path).text()).resolves.toBe("console.log('render')\n");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("copies a binary asset without decoding it", async () => {
|
|
291
|
+
const { bundle, project } = await makeDirectories();
|
|
292
|
+
const bytes = Uint8Array.from([0, 255, 17, 34]);
|
|
293
|
+
await writeFile(join(bundle, "assets", "template.bin"), bytes);
|
|
294
|
+
|
|
295
|
+
const result = await copySkillResource(
|
|
296
|
+
bundle,
|
|
297
|
+
"assets/template.bin",
|
|
298
|
+
project,
|
|
299
|
+
".pi/tools/template.bin",
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
expect(new Uint8Array(await Bun.file(result.path).arrayBuffer())).toEqual(bytes);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("allows references to be copied instead of returned to context", async () => {
|
|
306
|
+
const { bundle, project } = await makeDirectories();
|
|
307
|
+
await writeFile(join(bundle, "references", "long.md"), "large reference");
|
|
308
|
+
|
|
309
|
+
const result = await copySkillResource(
|
|
310
|
+
bundle,
|
|
311
|
+
"references/long.md",
|
|
312
|
+
project,
|
|
313
|
+
".pi/references/long.md",
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
await expect(Bun.file(result.path).text()).resolves.toBe("large reference");
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("rejects unsafe output paths", async () => {
|
|
320
|
+
const { bundle, project } = await makeDirectories();
|
|
321
|
+
await writeFile(join(bundle, "scripts", "render.ts"), "content");
|
|
322
|
+
|
|
323
|
+
await expect(
|
|
324
|
+
copySkillResource(bundle, "scripts/render.ts", project, "../outside.ts"),
|
|
325
|
+
).rejects.toThrow("Invalid output path");
|
|
326
|
+
await expect(
|
|
327
|
+
copySkillResource(bundle, "scripts/render.ts", project, "/tmp/outside.ts"),
|
|
328
|
+
).rejects.toThrow("Invalid output path");
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("rejects output parents that symlink outside the project", async () => {
|
|
332
|
+
const { bundle, project } = await makeDirectories();
|
|
333
|
+
const outside = await mkdtemp(join(tmpdir(), "pix-skill-copy-outside-"));
|
|
334
|
+
roots.push(outside);
|
|
335
|
+
await writeFile(join(bundle, "scripts", "render.ts"), "content");
|
|
336
|
+
await symlink(outside, join(project, "escape"));
|
|
337
|
+
|
|
338
|
+
await expect(
|
|
339
|
+
copySkillResource(bundle, "scripts/render.ts", project, "escape/render.ts"),
|
|
340
|
+
).rejects.toThrow("Invalid output path");
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("replaces an output symlink without writing through it", async () => {
|
|
344
|
+
const { bundle, project } = await makeDirectories();
|
|
345
|
+
const outside = await mkdtemp(join(tmpdir(), "pix-skill-copy-target-"));
|
|
346
|
+
roots.push(outside);
|
|
347
|
+
await writeFile(join(bundle, "scripts", "render.ts"), "safe content");
|
|
348
|
+
await writeFile(join(outside, "target.ts"), "outside content");
|
|
349
|
+
await symlink(join(outside, "target.ts"), join(project, "render.ts"));
|
|
350
|
+
|
|
351
|
+
await copySkillResource(bundle, "scripts/render.ts", project, "render.ts");
|
|
352
|
+
|
|
353
|
+
await expect(Bun.file(join(outside, "target.ts")).text()).resolves.toBe("outside content");
|
|
354
|
+
await expect(Bun.file(join(project, "render.ts")).text()).resolves.toBe("safe content");
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
97
358
|
describe("findCommandDirectives", () => {
|
|
98
359
|
it("matches !`cmd` with source span", () => {
|
|
99
360
|
const md = "x\n!`git status -s`\ny";
|
|
@@ -112,9 +373,10 @@ describe("findCommandDirectives", () => {
|
|
|
112
373
|
});
|
|
113
374
|
|
|
114
375
|
it("finds multiple", () => {
|
|
115
|
-
expect(
|
|
116
|
-
|
|
117
|
-
|
|
376
|
+
expect(findCommandDirectives("!`pwd` !`git diff`").map((d) => d.command)).toEqual([
|
|
377
|
+
"pwd",
|
|
378
|
+
"git diff",
|
|
379
|
+
]);
|
|
118
380
|
});
|
|
119
381
|
});
|
|
120
382
|
|
|
@@ -125,20 +387,7 @@ describe("replaceSpan", () => {
|
|
|
125
387
|
});
|
|
126
388
|
|
|
127
389
|
describe("hasShellMeta", () => {
|
|
128
|
-
for (const ch of [
|
|
129
|
-
";",
|
|
130
|
-
"|",
|
|
131
|
-
"&",
|
|
132
|
-
"$",
|
|
133
|
-
"`",
|
|
134
|
-
">",
|
|
135
|
-
"<",
|
|
136
|
-
"(",
|
|
137
|
-
")",
|
|
138
|
-
"{",
|
|
139
|
-
"}",
|
|
140
|
-
"\n",
|
|
141
|
-
]) {
|
|
390
|
+
for (const ch of [";", "|", "&", "$", "`", ">", "<", "(", ")", "{", "}", "\n"]) {
|
|
142
391
|
it(`flags ${JSON.stringify(ch)}`, () => {
|
|
143
392
|
expect(hasShellMeta(`git status ${ch}`)).toBe(true);
|
|
144
393
|
});
|
package/src/index.ts
CHANGED
|
@@ -12,11 +12,13 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
15
|
+
import { copyFile, mkdir, readFile, realpath, rename, rm, stat } from "node:fs/promises";
|
|
15
16
|
import { homedir } from "node:os";
|
|
16
|
-
import { join, resolve } from "node:path";
|
|
17
|
+
import { dirname, isAbsolute, join, relative, resolve, win32 } from "node:path";
|
|
17
18
|
import { fileURLToPath } from "node:url";
|
|
18
19
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
19
20
|
import { Text } from "@earendil-works/pi-tui";
|
|
21
|
+
import { type CollapseState, tickCollapse } from "@xynogen/pix-data/collapse";
|
|
20
22
|
import { Type } from "typebox";
|
|
21
23
|
import {
|
|
22
24
|
directiveBlockReason,
|
|
@@ -56,6 +58,8 @@ interface SkillEntry {
|
|
|
56
58
|
name: string;
|
|
57
59
|
/** Absolute path to the SKILL.md or flat .md file. */
|
|
58
60
|
path: string;
|
|
61
|
+
/** Absolute bundle directory, or null for a flat skill. */
|
|
62
|
+
root: string | null;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
/**
|
|
@@ -74,12 +78,12 @@ function scanSkillsDir(root: string): SkillEntry[] {
|
|
|
74
78
|
// Subdirectory layout: skills/<name>/SKILL.md
|
|
75
79
|
const skillMd = join(root, entry.name, "SKILL.md");
|
|
76
80
|
if (existsSync(skillMd)) {
|
|
77
|
-
entries.push({ name: entry.name, path: skillMd });
|
|
81
|
+
entries.push({ name: entry.name, path: skillMd, root: dirname(skillMd) });
|
|
78
82
|
}
|
|
79
83
|
} else if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
80
84
|
// Flat layout: skills/<name>.md
|
|
81
85
|
const name = entry.name.replace(/\.md$/, "");
|
|
82
|
-
entries.push({ name, path: join(root, entry.name) });
|
|
86
|
+
entries.push({ name, path: join(root, entry.name), root: null });
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -117,6 +121,116 @@ export function extractName(content: string): string | null {
|
|
|
117
121
|
return nm ? (nm[1]?.trim() ?? null) : null;
|
|
118
122
|
}
|
|
119
123
|
|
|
124
|
+
const RESOURCE_DIRECTORIES = new Set(["scripts", "references", "assets"]);
|
|
125
|
+
const MAX_TEXT_RESOURCE_BYTES = 1_048_576;
|
|
126
|
+
|
|
127
|
+
function resourceSegments(resource: string): string[] {
|
|
128
|
+
const invalid = () => new Error("Invalid resource path");
|
|
129
|
+
if (
|
|
130
|
+
!resource ||
|
|
131
|
+
resource.includes("\\") ||
|
|
132
|
+
resource.includes("\0") ||
|
|
133
|
+
isAbsolute(resource) ||
|
|
134
|
+
win32.isAbsolute(resource)
|
|
135
|
+
) {
|
|
136
|
+
throw invalid();
|
|
137
|
+
}
|
|
138
|
+
const segments = resource.split("/");
|
|
139
|
+
if (
|
|
140
|
+
!RESOURCE_DIRECTORIES.has(segments[0] ?? "") ||
|
|
141
|
+
segments.length < 2 ||
|
|
142
|
+
segments.some((segment) => !segment || segment === "." || segment === "..")
|
|
143
|
+
) {
|
|
144
|
+
throw invalid();
|
|
145
|
+
}
|
|
146
|
+
return segments;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function resolveSkillResource(skillRoot: string, resource: string): Promise<string> {
|
|
150
|
+
const invalid = () => new Error("Invalid resource path");
|
|
151
|
+
const segments = resourceSegments(resource);
|
|
152
|
+
let canonicalRoot: string;
|
|
153
|
+
let canonicalResource: string;
|
|
154
|
+
try {
|
|
155
|
+
canonicalRoot = await realpath(skillRoot);
|
|
156
|
+
const candidate = resolve(canonicalRoot, ...segments);
|
|
157
|
+
const lexicalRelative = relative(canonicalRoot, candidate);
|
|
158
|
+
if (lexicalRelative.startsWith("..") || isAbsolute(lexicalRelative)) throw invalid();
|
|
159
|
+
canonicalResource = await realpath(candidate);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error instanceof Error && error.message === "Invalid resource path") throw error;
|
|
162
|
+
throw new Error(`Resource not found: ${resource}`);
|
|
163
|
+
}
|
|
164
|
+
const canonicalRelative = relative(canonicalRoot, canonicalResource);
|
|
165
|
+
if (canonicalRelative.startsWith("..") || isAbsolute(canonicalRelative)) throw invalid();
|
|
166
|
+
const info = await stat(canonicalResource);
|
|
167
|
+
if (!info.isFile()) throw new Error(`Resource is not a file: ${resource}`);
|
|
168
|
+
return canonicalResource;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Read a references/ UTF-8 resource into model context. */
|
|
172
|
+
export async function readSkillResource(skillRoot: string, resource: string): Promise<string> {
|
|
173
|
+
const segments = resourceSegments(resource);
|
|
174
|
+
const source = await resolveSkillResource(skillRoot, resource);
|
|
175
|
+
if (segments[0] !== "references") {
|
|
176
|
+
throw new Error("Output is required for scripts/ and assets/ resources");
|
|
177
|
+
}
|
|
178
|
+
const info = await stat(source);
|
|
179
|
+
if (info.size > MAX_TEXT_RESOURCE_BYTES) {
|
|
180
|
+
throw new Error(`Resource exceeds ${MAX_TEXT_RESOURCE_BYTES} byte limit: ${resource}`);
|
|
181
|
+
}
|
|
182
|
+
return readFile(source, "utf-8");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export type CopiedSkillResource = { path: string; bytes: number };
|
|
186
|
+
|
|
187
|
+
/** Copy any conventional bundle resource as raw bytes into the caller project. */
|
|
188
|
+
export async function copySkillResource(
|
|
189
|
+
skillRoot: string,
|
|
190
|
+
resource: string,
|
|
191
|
+
projectRoot: string,
|
|
192
|
+
output: string,
|
|
193
|
+
): Promise<CopiedSkillResource> {
|
|
194
|
+
const invalid = () => new Error("Invalid output path");
|
|
195
|
+
if (
|
|
196
|
+
!output ||
|
|
197
|
+
output.includes("\\") ||
|
|
198
|
+
output.includes("\0") ||
|
|
199
|
+
isAbsolute(output) ||
|
|
200
|
+
win32.isAbsolute(output)
|
|
201
|
+
) {
|
|
202
|
+
throw invalid();
|
|
203
|
+
}
|
|
204
|
+
const outputSegments = output.split("/");
|
|
205
|
+
if (outputSegments.some((segment) => !segment || segment === "." || segment === "..")) {
|
|
206
|
+
throw invalid();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const source = await resolveSkillResource(skillRoot, resource);
|
|
210
|
+
const canonicalProject = await realpath(projectRoot);
|
|
211
|
+
const destination = resolve(canonicalProject, ...outputSegments);
|
|
212
|
+
const lexicalRelative = relative(canonicalProject, destination);
|
|
213
|
+
if (lexicalRelative.startsWith("..") || isAbsolute(lexicalRelative)) throw invalid();
|
|
214
|
+
|
|
215
|
+
const parent = dirname(destination);
|
|
216
|
+
await mkdir(parent, { recursive: true });
|
|
217
|
+
const canonicalParent = await realpath(parent);
|
|
218
|
+
const parentRelative = relative(canonicalProject, canonicalParent);
|
|
219
|
+
if (parentRelative.startsWith("..") || isAbsolute(parentRelative)) throw invalid();
|
|
220
|
+
|
|
221
|
+
const temporary = join(
|
|
222
|
+
canonicalParent,
|
|
223
|
+
`.pix-skill-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.tmp`,
|
|
224
|
+
);
|
|
225
|
+
try {
|
|
226
|
+
await copyFile(source, temporary);
|
|
227
|
+
await rename(temporary, destination);
|
|
228
|
+
} finally {
|
|
229
|
+
await rm(temporary, { force: true });
|
|
230
|
+
}
|
|
231
|
+
return { path: destination, bytes: (await stat(source)).size };
|
|
232
|
+
}
|
|
233
|
+
|
|
120
234
|
// ─── Command directive interpolation ───────────────────────────────────────────
|
|
121
235
|
|
|
122
236
|
export type ArgvRunner = (argv: string[], cwd: string) => Promise<string>;
|
|
@@ -170,6 +284,74 @@ export type ThemeLike = {
|
|
|
170
284
|
fg: (key: "accent" | "muted" | "toolTitle", text: string) => string;
|
|
171
285
|
};
|
|
172
286
|
|
|
287
|
+
export function formatSkillList(names: string[]): string {
|
|
288
|
+
return `Available skills (${names.length}): ${names.join(" · ")}`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export type SkillCallArgs = {
|
|
292
|
+
name?: string;
|
|
293
|
+
full?: boolean;
|
|
294
|
+
resource?: string;
|
|
295
|
+
output?: string;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
export type SkillResultDetails =
|
|
299
|
+
| { mode: "list"; count: number }
|
|
300
|
+
| { mode: "description"; name: string }
|
|
301
|
+
| { mode: "instructions"; name: string; lines: number }
|
|
302
|
+
| { mode: "reference"; name: string; resource: string; bytes: number }
|
|
303
|
+
| { mode: "copy"; name: string; resource: string; output: string; bytes: number };
|
|
304
|
+
|
|
305
|
+
function formatBytes(bytes: number): string {
|
|
306
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
307
|
+
return `${(bytes / 1024).toFixed(1)} KiB`;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function formatSkillCallLabel(args: SkillCallArgs): string {
|
|
311
|
+
if (!args.name) return "list";
|
|
312
|
+
if (args.resource && args.output) {
|
|
313
|
+
return `copy · ${args.name}/${args.resource} → ${args.output}`;
|
|
314
|
+
}
|
|
315
|
+
if (args.resource) return `reference · ${args.name}/${args.resource}`;
|
|
316
|
+
if (args.full) return `instructions · ${args.name}`;
|
|
317
|
+
return `description · ${args.name}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function formatCollapsedSkillResult(details: SkillResultDetails): string {
|
|
321
|
+
switch (details.mode) {
|
|
322
|
+
case "list":
|
|
323
|
+
return `${details.count} skills`;
|
|
324
|
+
case "description":
|
|
325
|
+
return `${details.name} · description`;
|
|
326
|
+
case "instructions":
|
|
327
|
+
return `${details.name} · ${details.lines} instruction lines`;
|
|
328
|
+
case "reference":
|
|
329
|
+
return `${details.name} · reference · ${formatBytes(details.bytes)}`;
|
|
330
|
+
case "copy":
|
|
331
|
+
return `copied · ${details.output} · ${formatBytes(details.bytes)}`;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function formatExpandedSkillResult(details: SkillResultDetails, text: string): string {
|
|
336
|
+
switch (details.mode) {
|
|
337
|
+
case "list":
|
|
338
|
+
return text;
|
|
339
|
+
case "description": {
|
|
340
|
+
const prefix = `${details.name}:`;
|
|
341
|
+
const description = text.startsWith(prefix) ? text.slice(prefix.length).trimStart() : text;
|
|
342
|
+
return `DESCRIPTION · ${details.name}\n${description}`;
|
|
343
|
+
}
|
|
344
|
+
case "instructions": {
|
|
345
|
+
const preview = text.length > 100 ? `${text.slice(0, 100)}...` : text;
|
|
346
|
+
return `INSTRUCTIONS · ${details.name} · ${details.lines} lines\n${preview}`;
|
|
347
|
+
}
|
|
348
|
+
case "reference":
|
|
349
|
+
return `REFERENCE · ${details.name}\n${details.resource} · ${formatBytes(details.bytes)}\n${text}`;
|
|
350
|
+
case "copy":
|
|
351
|
+
return `COPIED · ${details.name}\n${details.resource} → ${details.output} · ${formatBytes(details.bytes)}`;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
173
355
|
export function formatSkillSummary(text: string, theme: ThemeLike): string {
|
|
174
356
|
const trimmed = text.trim();
|
|
175
357
|
if (!trimmed) return theme.fg("muted", "No skills found.");
|
|
@@ -198,8 +380,7 @@ export function formatSkillSummary(text: string, theme: ThemeLike): string {
|
|
|
198
380
|
const ParamsSchema = Type.Object({
|
|
199
381
|
name: Type.Optional(
|
|
200
382
|
Type.String({
|
|
201
|
-
description:
|
|
202
|
-
'Skill name, e.g. "commit", "debug". Omit to list all skills.',
|
|
383
|
+
description: 'Skill name, e.g. "commit", "debug". Omit to list all skills.',
|
|
203
384
|
}),
|
|
204
385
|
),
|
|
205
386
|
full: Type.Optional(
|
|
@@ -209,6 +390,18 @@ const ParamsSchema = Type.Object({
|
|
|
209
390
|
"When true, return the full SKILL.md content. When false (default), return the description only.",
|
|
210
391
|
}),
|
|
211
392
|
),
|
|
393
|
+
resource: Type.Optional(
|
|
394
|
+
Type.String({
|
|
395
|
+
description:
|
|
396
|
+
"Bundle-relative file under scripts/, references/, or assets/. Scripts/assets require output; references may be read directly.",
|
|
397
|
+
}),
|
|
398
|
+
),
|
|
399
|
+
output: Type.Optional(
|
|
400
|
+
Type.String({
|
|
401
|
+
description:
|
|
402
|
+
"Project-relative destination for copying the resource as raw bytes. Required for scripts/ and assets/; optional for references/.",
|
|
403
|
+
}),
|
|
404
|
+
),
|
|
212
405
|
});
|
|
213
406
|
|
|
214
407
|
function registerSkillLoader(pi: ExtensionAPI): void {
|
|
@@ -216,22 +409,18 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
216
409
|
name: "read_skills",
|
|
217
410
|
label: "Read Skills",
|
|
218
411
|
description:
|
|
219
|
-
"Browse and
|
|
412
|
+
"Browse skills and access conventional bundle resources. References can be read into context; scripts/assets must be copied to a project-relative output path before use.",
|
|
220
413
|
promptSnippet: "Browse and load bundled skill instructions",
|
|
221
414
|
promptGuidelines: [
|
|
222
|
-
"
|
|
223
|
-
"Call read_skills() with no arguments to list all available skills and their descriptions.",
|
|
224
|
-
"Call read_skills(name=<skill>) to read the description of a specific skill before deciding to load it.",
|
|
225
|
-
"Call read_skills(name=<skill>, full=true) to load the full procedure for a skill before executing it.",
|
|
226
|
-
"Prefer read_skills() over the read tool for skills — it resolves the correct path regardless of install location.",
|
|
415
|
+
"Before any task, call read_skills(); load a matching skill in full before acting.",
|
|
227
416
|
],
|
|
228
417
|
executionMode: "sequential",
|
|
229
418
|
parameters: ParamsSchema,
|
|
230
419
|
|
|
231
420
|
async execute(_toolCallId, params, _signal, _upd, toolCtx) {
|
|
232
|
-
const ok = (text: string) => ({
|
|
421
|
+
const ok = (text: string, details: SkillResultDetails) => ({
|
|
233
422
|
content: [{ type: "text" as const, text }],
|
|
234
|
-
details
|
|
423
|
+
details,
|
|
235
424
|
});
|
|
236
425
|
const fail = (text: string) => ({
|
|
237
426
|
content: [{ type: "text" as const, text }],
|
|
@@ -239,69 +428,90 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
239
428
|
isError: true,
|
|
240
429
|
});
|
|
241
430
|
|
|
242
|
-
const { name, full } = params as {
|
|
431
|
+
const { name, full, resource, output } = params as {
|
|
432
|
+
name?: string;
|
|
433
|
+
full?: boolean;
|
|
434
|
+
resource?: string;
|
|
435
|
+
output?: string;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
if (resource && !name) return fail("A skill name is required to access a resource.");
|
|
439
|
+
if (output && !resource) return fail("A resource is required when output is provided.");
|
|
243
440
|
|
|
244
441
|
// No name → list all skills
|
|
245
442
|
if (!name) {
|
|
246
443
|
const skills = discoverSkills();
|
|
247
|
-
if (!skills.length) return ok("No skills found.");
|
|
248
|
-
|
|
249
|
-
const lines = skills.map((s) => {
|
|
250
|
-
try {
|
|
251
|
-
const content = readFileSync(s.path, "utf-8");
|
|
252
|
-
const desc = extractDescription(content);
|
|
253
|
-
return desc ? `${s.name}: ${desc}` : s.name;
|
|
254
|
-
} catch {
|
|
255
|
-
return s.name;
|
|
256
|
-
}
|
|
257
|
-
});
|
|
444
|
+
if (!skills.length) return ok("No skills found.", { mode: "list", count: 0 });
|
|
258
445
|
|
|
259
|
-
return ok(
|
|
260
|
-
|
|
261
|
-
|
|
446
|
+
return ok(formatSkillList(skills.map((skill) => skill.name)), {
|
|
447
|
+
mode: "list",
|
|
448
|
+
count: skills.length,
|
|
449
|
+
});
|
|
262
450
|
}
|
|
263
451
|
|
|
264
452
|
// Resolve skill
|
|
265
453
|
const skills = discoverSkills();
|
|
266
|
-
const entry = skills.find(
|
|
267
|
-
(s) => s.name === name || s.name === name.replace(/\.md$/, ""),
|
|
268
|
-
);
|
|
454
|
+
const entry = skills.find((s) => s.name === name || s.name === name.replace(/\.md$/, ""));
|
|
269
455
|
|
|
270
456
|
if (!entry) {
|
|
271
457
|
const names = skills.map((s) => s.name).join(", ");
|
|
272
|
-
return fail(
|
|
273
|
-
`Skill "${name}" not found. Available: ${names || "(none)"}`,
|
|
274
|
-
);
|
|
458
|
+
return fail(`Skill "${name}" not found. Available: ${names || "(none)"}`);
|
|
275
459
|
}
|
|
276
460
|
|
|
277
461
|
try {
|
|
462
|
+
if (resource) {
|
|
463
|
+
if (!entry.root) {
|
|
464
|
+
return fail(`Skill "${entry.name}" uses the flat layout and has no bundled resources.`);
|
|
465
|
+
}
|
|
466
|
+
if (output) {
|
|
467
|
+
const cwd = (toolCtx as { cwd?: string })?.cwd ?? process.cwd();
|
|
468
|
+
const copied = await copySkillResource(entry.root, resource, cwd, output);
|
|
469
|
+
return ok(`Copied ${resource} to ${output} (${copied.bytes} bytes).`, {
|
|
470
|
+
mode: "copy",
|
|
471
|
+
name: entry.name,
|
|
472
|
+
resource,
|
|
473
|
+
output,
|
|
474
|
+
bytes: copied.bytes,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
const reference = await readSkillResource(entry.root, resource);
|
|
478
|
+
return ok(reference, {
|
|
479
|
+
mode: "reference",
|
|
480
|
+
name: entry.name,
|
|
481
|
+
resource,
|
|
482
|
+
bytes: Buffer.byteLength(reference, "utf-8"),
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
278
486
|
const content = readFileSync(entry.path, "utf-8");
|
|
279
487
|
|
|
280
488
|
// full=false (default) → description only
|
|
281
489
|
if (!full) {
|
|
282
490
|
const desc = extractDescription(content);
|
|
283
|
-
return ok(
|
|
284
|
-
|
|
285
|
-
|
|
491
|
+
return ok(desc ? `${entry.name}: ${desc}` : `${entry.name}: (no description)`, {
|
|
492
|
+
mode: "description",
|
|
493
|
+
name: entry.name,
|
|
494
|
+
});
|
|
286
495
|
}
|
|
287
496
|
|
|
288
497
|
// full=true → interpolate !`cmd` directives (pix-gate-gated, no
|
|
289
498
|
// prompt; auto-deny on any rule match), then return.
|
|
290
499
|
const cwd = (toolCtx as { cwd?: string })?.cwd ?? process.cwd();
|
|
291
500
|
const expanded = await interpolateSkill(content, cwd);
|
|
292
|
-
return ok(expanded
|
|
501
|
+
return ok(expanded, {
|
|
502
|
+
mode: "instructions",
|
|
503
|
+
name: entry.name,
|
|
504
|
+
lines: expanded.split(/\r?\n/).length,
|
|
505
|
+
});
|
|
293
506
|
} catch (err) {
|
|
294
507
|
return fail(
|
|
295
|
-
`Failed to read skill "${name}": ${
|
|
296
|
-
err instanceof Error ? err.message : String(err)
|
|
297
|
-
}`,
|
|
508
|
+
`Failed to read skill "${name}": ${err instanceof Error ? err.message : String(err)}`,
|
|
298
509
|
);
|
|
299
510
|
}
|
|
300
511
|
},
|
|
301
512
|
|
|
302
513
|
renderCall(args, theme) {
|
|
303
|
-
const
|
|
304
|
-
const label = name ? `${name}${full ? " (full)" : ""}` : "list";
|
|
514
|
+
const label = formatSkillCallLabel(args as SkillCallArgs);
|
|
305
515
|
return new Text(
|
|
306
516
|
`${theme.fg("toolTitle", theme.bold("read_skills"))} ${theme.fg("muted", label)}`,
|
|
307
517
|
0,
|
|
@@ -309,12 +519,27 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
309
519
|
);
|
|
310
520
|
},
|
|
311
521
|
|
|
312
|
-
renderResult(result, _options, theme) {
|
|
522
|
+
renderResult(result, _options, theme, renderCtx) {
|
|
313
523
|
const text = result.content
|
|
314
524
|
?.filter((content) => content.type === "text")
|
|
315
525
|
.map((content) => content.text || "")
|
|
316
526
|
.join("\n");
|
|
317
|
-
|
|
527
|
+
const component =
|
|
528
|
+
renderCtx.lastComponent instanceof Text ? renderCtx.lastComponent : new Text("", 0, 0);
|
|
529
|
+
const details = result.details as SkillResultDetails | undefined;
|
|
530
|
+
if (!renderCtx.isError && details) {
|
|
531
|
+
const state = renderCtx.state as CollapseState;
|
|
532
|
+
if (tickCollapse("read_skills", state, renderCtx.invalidate)) {
|
|
533
|
+
component.setText(theme.fg("muted", formatCollapsedSkillResult(details)));
|
|
534
|
+
return component;
|
|
535
|
+
}
|
|
536
|
+
component.setText(
|
|
537
|
+
formatSkillSummary(formatExpandedSkillResult(details, text ?? ""), theme),
|
|
538
|
+
);
|
|
539
|
+
return component;
|
|
540
|
+
}
|
|
541
|
+
component.setText(formatSkillSummary(text ?? "", theme));
|
|
542
|
+
return component;
|
|
318
543
|
},
|
|
319
544
|
});
|
|
320
545
|
}
|
package/src/run.test.ts
CHANGED
|
@@ -3,9 +3,7 @@ import { runArgv } from "./run.ts";
|
|
|
3
3
|
|
|
4
4
|
describe("runArgv", () => {
|
|
5
5
|
it("captures stdout", async () => {
|
|
6
|
-
expect(
|
|
7
|
-
(await runArgv(["echo", "hello"], { cwd: process.cwd() })).trim(),
|
|
8
|
-
).toBe("hello");
|
|
6
|
+
expect((await runArgv(["echo", "hello"], { cwd: process.cwd() })).trim()).toBe("hello");
|
|
9
7
|
});
|
|
10
8
|
|
|
11
9
|
it("caps output at maxBytes", async () => {
|
package/src/run.ts
CHANGED
|
@@ -40,9 +40,7 @@ export function runArgv(argv: string[], opts: RunOptions): Promise<string> {
|
|
|
40
40
|
stdio: ["ignore", "pipe", "pipe"],
|
|
41
41
|
});
|
|
42
42
|
} catch (err) {
|
|
43
|
-
finish(
|
|
44
|
-
`[command failed: ${err instanceof Error ? err.message : String(err)}]`,
|
|
45
|
-
);
|
|
43
|
+
finish(`[command failed: ${err instanceof Error ? err.message : String(err)}]`);
|
|
46
44
|
return;
|
|
47
45
|
}
|
|
48
46
|
|
|
@@ -73,7 +71,6 @@ function format(
|
|
|
73
71
|
truncMarker = "[output truncated]",
|
|
74
72
|
): string {
|
|
75
73
|
const combined = Buffer.concat(chunks).toString("utf-8").trimEnd();
|
|
76
|
-
if (bytes > maxBytes)
|
|
77
|
-
return `${combined.slice(0, maxBytes)}\n… ${truncMarker}`;
|
|
74
|
+
if (bytes > maxBytes) return `${combined.slice(0, maxBytes)}\n… ${truncMarker}`;
|
|
78
75
|
return combined || "(no output)";
|
|
79
76
|
}
|