pi-better-subagents 0.27.2 → 0.28.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 +20 -8
- package/agent-commands.ts +29 -10
- package/agent-inspection.ts +116 -5
- package/agents-catalog-tool.ts +5 -3
- package/batch.mjs +6 -0
- package/catalog-resolver.ts +28 -0
- package/catalog-runtime.ts +8 -0
- package/catalog-schema.ts +110 -8
- package/catalog-skill-edits.ts +116 -0
- package/catalog-ui.ts +172 -27
- package/child-ownership.ts +7 -1
- package/config.ts +35 -3
- package/continuation.ts +3 -0
- package/docs/agent-catalog-operations.md +47 -4
- package/docs/agent-catalog.md +12 -1
- package/docs/usage.md +81 -8
- package/index.ts +212 -8
- package/package.json +1 -1
- package/registry.ts +11 -0
- package/settings-page.ts +220 -47
- package/shared-worktree-context.ts +158 -0
- package/skill-picker.ts +349 -0
- package/skills.ts +183 -0
package/skill-picker.ts
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared searchable skill checkbox picker.
|
|
3
|
+
*
|
|
4
|
+
* Both selected-skill entry points use this component: `/subagents settings`
|
|
5
|
+
* for the global default and `/agents` for a role or named agent. It only
|
|
6
|
+
* renders metadata and returns paths; it never loads a model or writes files.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync } from "node:fs";
|
|
9
|
+
import { basename, dirname, isAbsolute, resolve } from "node:path";
|
|
10
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import * as Tui from "@earendil-works/pi-tui";
|
|
12
|
+
|
|
13
|
+
/** Metadata Pi already loaded for the current session. */
|
|
14
|
+
export interface DiscoveredSkill {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
/** Absolute path to SKILL.md. */
|
|
18
|
+
path: string;
|
|
19
|
+
/** Skill directory, when Pi reports one. */
|
|
20
|
+
baseDir?: string;
|
|
21
|
+
/** Resource source, such as a package or user directory. */
|
|
22
|
+
source?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SkillPickerApplyResult {
|
|
26
|
+
ok: boolean;
|
|
27
|
+
message?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SkillPickerOptions {
|
|
31
|
+
theme: Theme;
|
|
32
|
+
skills: readonly DiscoveredSkill[];
|
|
33
|
+
selected: readonly string[];
|
|
34
|
+
requestRender: () => void;
|
|
35
|
+
/** Caller applies the draft. A failed apply keeps the picker and checked items. */
|
|
36
|
+
apply(paths: string[]): SkillPickerApplyResult;
|
|
37
|
+
finish(): void;
|
|
38
|
+
cancel(): void;
|
|
39
|
+
getHeight?: () => number;
|
|
40
|
+
title?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type Row =
|
|
44
|
+
| { kind: "skill"; skill: DiscoveredSkill; key: string }
|
|
45
|
+
| { kind: "extra"; path: string; key: string }
|
|
46
|
+
| { kind: "apply" }
|
|
47
|
+
| { kind: "add" };
|
|
48
|
+
|
|
49
|
+
function pathKey(path: string): string {
|
|
50
|
+
try { return resolve(path); }
|
|
51
|
+
catch { return path; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function skillKeys(skill: DiscoveredSkill): string[] {
|
|
55
|
+
return [pathKey(skill.path), ...(skill.baseDir ? [pathKey(skill.baseDir)] : [])];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function unavailableName(path: string): string {
|
|
59
|
+
const parent = basename(dirname(path));
|
|
60
|
+
return parent || basename(path);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function errorText(error: unknown): string {
|
|
64
|
+
return error instanceof Error ? error.message : String(error);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function cell(text: string, width: number): string {
|
|
68
|
+
const clipped = Tui.truncateToWidth(text, width, "");
|
|
69
|
+
return clipped + " ".repeat(Math.max(0, width - Tui.visibleWidth(clipped)));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function createSkillPicker(options: SkillPickerOptions): Tui.Component & Tui.Focusable {
|
|
73
|
+
const { theme } = options;
|
|
74
|
+
const search = new Tui.Input();
|
|
75
|
+
const pathInput = new Tui.Input();
|
|
76
|
+
let focused = false;
|
|
77
|
+
let cursor = 0;
|
|
78
|
+
let adding = false;
|
|
79
|
+
let message = "";
|
|
80
|
+
let error = false;
|
|
81
|
+
let scroll = 0;
|
|
82
|
+
|
|
83
|
+
const checked = new Map<string, string>();
|
|
84
|
+
for (const path of options.selected) checked.set(pathKey(path), path);
|
|
85
|
+
// Keep saved/manual rows available even after they are unchecked.
|
|
86
|
+
const extraPaths = new Map(checked);
|
|
87
|
+
|
|
88
|
+
const skills = [...options.skills].sort((left, right) =>
|
|
89
|
+
left.name.localeCompare(right.name) || left.path.localeCompare(right.path));
|
|
90
|
+
|
|
91
|
+
function request(): void {
|
|
92
|
+
options.requestRender();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function checkedKeysFor(skill: DiscoveredSkill): string[] {
|
|
96
|
+
const keys = new Set(skillKeys(skill));
|
|
97
|
+
return [...checked.keys()].filter((key) => keys.has(key));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isChecked(skill: DiscoveredSkill): boolean {
|
|
101
|
+
return checkedKeysFor(skill).length > 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function matches(value: string, query: string): boolean {
|
|
105
|
+
return value.toLowerCase().includes(query);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function rows(): Row[] {
|
|
109
|
+
const query = search.getValue().trim().toLowerCase();
|
|
110
|
+
const available = skills
|
|
111
|
+
.filter((skill) => !query || matches(skill.name, query) || matches(skill.description, query) || matches(skill.path, query))
|
|
112
|
+
.map((skill): Row => ({ kind: "skill", skill, key: skillKeys(skill)[0]! }));
|
|
113
|
+
const known = new Set(skills.flatMap(skillKeys));
|
|
114
|
+
const extras = [...extraPaths.entries()]
|
|
115
|
+
.filter(([key, path]) => !known.has(key)
|
|
116
|
+
&& (!query || matches(path, query) || matches(unavailableName(path), query)))
|
|
117
|
+
.map(([key, path]): Row => ({ kind: "extra", path, key }));
|
|
118
|
+
return [...available, ...extras, { kind: "apply" }, { kind: "add" }];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function current(entries = rows()): Row | undefined {
|
|
122
|
+
return entries[Math.min(cursor, entries.length - 1)];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function report(text: string, failed = false): void {
|
|
126
|
+
message = text;
|
|
127
|
+
error = failed;
|
|
128
|
+
request();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function toggle(row: Row | undefined): void {
|
|
132
|
+
if (!row || row.kind === "apply" || row.kind === "add") return;
|
|
133
|
+
if (row.kind === "skill") {
|
|
134
|
+
const keys = checkedKeysFor(row.skill);
|
|
135
|
+
if (keys.length > 0) keys.forEach((key) => checked.delete(key));
|
|
136
|
+
else checked.set(pathKey(row.skill.path), row.skill.path);
|
|
137
|
+
} else {
|
|
138
|
+
if (checked.has(row.key)) checked.delete(row.key);
|
|
139
|
+
else checked.set(row.key, row.path);
|
|
140
|
+
}
|
|
141
|
+
request();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function apply(): void {
|
|
145
|
+
const result = options.apply([...checked.values()]);
|
|
146
|
+
if (result.ok) {
|
|
147
|
+
options.finish();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
report(result.message ?? "Skills were not applied.", true);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function startAdd(): void {
|
|
154
|
+
adding = true;
|
|
155
|
+
pathInput.setValue("");
|
|
156
|
+
pathInput.handleInput("\x05");
|
|
157
|
+
pathInput.focused = focused;
|
|
158
|
+
message = "";
|
|
159
|
+
error = false;
|
|
160
|
+
request();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function cancelAdd(): void {
|
|
164
|
+
adding = false;
|
|
165
|
+
pathInput.focused = false;
|
|
166
|
+
message = "Add path cancelled.";
|
|
167
|
+
error = false;
|
|
168
|
+
request();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function submitAdd(value: string): void {
|
|
172
|
+
const path = value.trim();
|
|
173
|
+
if (!path) {
|
|
174
|
+
report("Enter an absolute path to a SKILL.md file or skill directory. Esc cancels.", true);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (!isAbsolute(path)) {
|
|
178
|
+
report("Enter an absolute path so every child resolves the same skill.", true);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (!existsSync(path)) {
|
|
182
|
+
report("No file or directory exists at that path.", true);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const key = pathKey(path);
|
|
186
|
+
if (checked.has(key)) {
|
|
187
|
+
report("That skill path is already selected.", true);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
checked.set(key, path);
|
|
191
|
+
extraPaths.set(key, path);
|
|
192
|
+
adding = false;
|
|
193
|
+
pathInput.focused = false;
|
|
194
|
+
message = "Skill path added. Press Enter to apply.";
|
|
195
|
+
error = false;
|
|
196
|
+
request();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
pathInput.onSubmit = submitAdd;
|
|
200
|
+
pathInput.onEscape = cancelAdd;
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
get focused() { return focused; },
|
|
204
|
+
set focused(value: boolean) {
|
|
205
|
+
focused = value;
|
|
206
|
+
search.focused = value && !adding;
|
|
207
|
+
pathInput.focused = value && adding;
|
|
208
|
+
},
|
|
209
|
+
invalidate() {
|
|
210
|
+
search.invalidate();
|
|
211
|
+
pathInput.invalidate();
|
|
212
|
+
},
|
|
213
|
+
handleInput(data: string) {
|
|
214
|
+
if (adding) {
|
|
215
|
+
if (Tui.matchesKey(data, Tui.Key.escape)) cancelAdd();
|
|
216
|
+
else {
|
|
217
|
+
pathInput.handleInput(data);
|
|
218
|
+
request();
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const entries = rows();
|
|
223
|
+
if (Tui.matchesKey(data, Tui.Key.escape)) {
|
|
224
|
+
options.cancel();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (Tui.matchesKey(data, Tui.Key.up)) {
|
|
228
|
+
cursor = Math.max(0, Math.min(cursor, entries.length - 1) - 1);
|
|
229
|
+
request();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (Tui.matchesKey(data, Tui.Key.down)) {
|
|
233
|
+
cursor = Math.min(entries.length - 1, Math.min(cursor, entries.length - 1) + 1);
|
|
234
|
+
request();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (Tui.matchesKey(data, Tui.Key.space)) {
|
|
238
|
+
toggle(current(entries));
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (Tui.matchesKey(data, Tui.Key.enter)) {
|
|
242
|
+
const row = current(entries);
|
|
243
|
+
if (row?.kind === "add") startAdd();
|
|
244
|
+
else apply();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
search.handleInput(data);
|
|
248
|
+
cursor = 0;
|
|
249
|
+
scroll = 0;
|
|
250
|
+
request();
|
|
251
|
+
},
|
|
252
|
+
render(width: number) {
|
|
253
|
+
const w = Math.max(1, Math.floor(width));
|
|
254
|
+
const height = Math.max(1, Math.floor(options.getHeight?.() ?? process.stdout.rows ?? 30));
|
|
255
|
+
const title = options.title ?? "Select skills";
|
|
256
|
+
const entries = rows();
|
|
257
|
+
cursor = Math.max(0, Math.min(cursor, entries.length - 1));
|
|
258
|
+
const marker = (isSelected: boolean) => isSelected ? "> " : " ";
|
|
259
|
+
const highlight = (text: string, isSelected: boolean): string => isSelected
|
|
260
|
+
? theme.bg("selectedBg", theme.bold?.(cell(text, w)) ?? cell(text, w))
|
|
261
|
+
: text;
|
|
262
|
+
const skillLines = (skill: DiscoveredSkill, ticked: boolean, isSelected: boolean): string[] => {
|
|
263
|
+
const mark = marker(isSelected);
|
|
264
|
+
const box = `[${ticked ? "x" : " "}] `;
|
|
265
|
+
if (w >= 64) {
|
|
266
|
+
const nameWidth = Math.min(30, Math.max(12, Math.floor(w * 0.3)));
|
|
267
|
+
const text = `${mark}${box}${cell(skill.name, nameWidth)}${theme.fg("dim", skill.description)}`;
|
|
268
|
+
return [highlight(text, isSelected)];
|
|
269
|
+
}
|
|
270
|
+
return [
|
|
271
|
+
highlight(theme.fg(isSelected ? "accent" : "text", `${mark}${box}${skill.name}`), isSelected),
|
|
272
|
+
theme.fg("dim", ` ${skill.description}`),
|
|
273
|
+
];
|
|
274
|
+
};
|
|
275
|
+
const extraLines = (path: string, isSelected: boolean): string[] => {
|
|
276
|
+
const mark = marker(isSelected);
|
|
277
|
+
return [
|
|
278
|
+
highlight(theme.fg(isSelected ? "accent" : "text", `${mark}[${checked.has(pathKey(path)) ? "x" : " "}] ${unavailableName(path)} (unavailable)`), isSelected),
|
|
279
|
+
theme.fg("dim", ` ${Tui.truncateToWidth(path, Math.max(1, w - 4), "")}`),
|
|
280
|
+
];
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const header: string[] = [
|
|
284
|
+
theme.fg("accent", theme.bold?.(` ${title}`) ?? ` ${title}`),
|
|
285
|
+
theme.fg("dim", " Up/Down Move · Space Toggle · Enter Apply · Esc Cancel"),
|
|
286
|
+
];
|
|
287
|
+
if (adding) {
|
|
288
|
+
header.push("", theme.fg("text", " Add skill path"), theme.fg("dim", " Absolute SKILL.md or skill directory · Enter add · Esc cancel"));
|
|
289
|
+
header.push(" " + pathOrSearch(pathInput, Math.max(1, w - 2)));
|
|
290
|
+
} else {
|
|
291
|
+
const label = " Search ";
|
|
292
|
+
const searchWidth = Math.max(1, w - Tui.visibleWidth(label));
|
|
293
|
+
header.push("", label + pathOrSearch(search, searchWidth));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const body: string[] = [];
|
|
297
|
+
if (skills.length === 0) {
|
|
298
|
+
body.push(theme.fg("dim", " No loaded skills were reported; use Add skill path."));
|
|
299
|
+
}
|
|
300
|
+
if (entries.every((row) => row.kind === "apply" || row.kind === "add") && search.getValue().trim()) {
|
|
301
|
+
body.push(theme.fg("dim", " No skills match this filter."));
|
|
302
|
+
}
|
|
303
|
+
let selectedLine = 0;
|
|
304
|
+
const visibleChecked = new Set<string>();
|
|
305
|
+
for (const [index, row] of entries.entries()) {
|
|
306
|
+
if (index === cursor) selectedLine = body.length;
|
|
307
|
+
if (row.kind === "skill") {
|
|
308
|
+
const ticked = isChecked(row.skill);
|
|
309
|
+
if (ticked) {
|
|
310
|
+
for (const key of checkedKeysFor(row.skill)) visibleChecked.add(key);
|
|
311
|
+
}
|
|
312
|
+
body.push(...skillLines(row.skill, ticked, index === cursor));
|
|
313
|
+
} else if (row.kind === "extra") {
|
|
314
|
+
if (checked.has(row.key)) visibleChecked.add(row.key);
|
|
315
|
+
body.push(...extraLines(row.path, index === cursor));
|
|
316
|
+
} else if (row.kind === "apply") {
|
|
317
|
+
body.push(highlight(theme.fg(index === cursor ? "accent" : "text", marker(index === cursor) + `✓ Apply selection (${checked.size} checked)`), index === cursor));
|
|
318
|
+
} else {
|
|
319
|
+
body.push(highlight(theme.fg(index === cursor ? "accent" : "text", marker(index === cursor) + "+ Add skill path…"), index === cursor));
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const hiddenChecked = [...checked.keys()].filter((key) => !visibleChecked.has(key)).length;
|
|
323
|
+
const footer = [
|
|
324
|
+
theme.fg("dim", ` ${checked.size} checked${hiddenChecked ? ` · ${hiddenChecked} checked hidden by filter` : ""}`),
|
|
325
|
+
theme.fg("dim", " 0 checked applies No skill catalog, not discovery."),
|
|
326
|
+
];
|
|
327
|
+
if (message) footer.unshift(theme.fg(error ? "error" : "muted", " " + message.replace(/[\r\n]+/g, " ")));
|
|
328
|
+
if (height < header.length + footer.length + 1) {
|
|
329
|
+
const active = adding ? [header[header.length - 1]!, ...(message ? [footer[0]!] : [])]
|
|
330
|
+
: height === 1 ? [body[selectedLine]!]
|
|
331
|
+
: [header[header.length - 1]!, body[selectedLine]!];
|
|
332
|
+
return active.slice(0, height).map((line) => Tui.truncateToWidth(line, w, ""));
|
|
333
|
+
}
|
|
334
|
+
const room = Math.max(1, height - header.length - footer.length);
|
|
335
|
+
if (selectedLine < scroll) scroll = selectedLine;
|
|
336
|
+
if (selectedLine >= scroll + room) scroll = selectedLine - room + 1;
|
|
337
|
+
scroll = Math.max(0, Math.min(scroll, Math.max(0, body.length - room)));
|
|
338
|
+
const lines = [...header, ...body.slice(scroll, scroll + room), ...footer];
|
|
339
|
+
if (height < lines.length) lines.splice(0, Math.max(0, lines.length - height));
|
|
340
|
+
return lines.map((line) => Tui.truncateToWidth(line, w, ""));
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
function pathOrSearch(input: Tui.Input, width: number): string {
|
|
345
|
+
return input.render(width)[0] ?? "";
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
}
|
package/skills.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skill exposure for one subagent launch (#664).
|
|
3
|
+
*
|
|
4
|
+
* A child launched by `subagent_spawn` otherwise inherits the whole user-level
|
|
5
|
+
* skills catalog, which is most of a long system prompt even when the task names
|
|
6
|
+
* the one `SKILL.md` it needs by path. Pi's CLI has exactly three observable
|
|
7
|
+
* states, and this module maps a launch onto them:
|
|
8
|
+
*
|
|
9
|
+
* | Choice | Child argv | Meaning |
|
|
10
|
+
* | --- | --- | --- |
|
|
11
|
+
* | absent | (no flag) | discovery of the user catalog |
|
|
12
|
+
* | `[]` | `--no-skills` | no skills at all |
|
|
13
|
+
* | `["/abs/SKILL.md", …]` | `--no-skills --skill <path>…` | exactly those skills |
|
|
14
|
+
*
|
|
15
|
+
* An absent value therefore means "inherit", an empty array means "none", and a
|
|
16
|
+
* non-empty array means "exactly these". The three cases stay distinct because
|
|
17
|
+
* `[]` is a real choice, not a missing one.
|
|
18
|
+
*
|
|
19
|
+
* `exclude_skills` is deliberately not part of this contract: Pi cannot remove a
|
|
20
|
+
* single entry from a discovered catalog without also taking over discovery.
|
|
21
|
+
*/
|
|
22
|
+
import { isAbsolute, resolve } from "node:path";
|
|
23
|
+
|
|
24
|
+
export type SkillExposureMode = "inherit" | "none" | "explicit";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Where the effective choice came from. `recipe` is a continuation replaying a
|
|
28
|
+
* frozen launch, and `default` is the ultimate fallback: discovery.
|
|
29
|
+
*/
|
|
30
|
+
export type SkillExposureSource = "launch" | "catalog" | "session" | "config" | "default" | "recipe";
|
|
31
|
+
|
|
32
|
+
export interface SkillExposure {
|
|
33
|
+
mode: SkillExposureMode;
|
|
34
|
+
/** Absolute paths, in the order the flag appears. Empty unless `explicit`. */
|
|
35
|
+
paths: readonly string[];
|
|
36
|
+
source: SkillExposureSource;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ConfiguredSkillExposure {
|
|
40
|
+
mode: SkillExposureMode;
|
|
41
|
+
paths: string[];
|
|
42
|
+
/** `config` when the key is present (including null/[]), `default` when absent. */
|
|
43
|
+
source: "config" | "default";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Turn the saved global `defaultSkills` value into the same three states a
|
|
48
|
+
* launch uses. An absent key and `null` both mean discovery; `[]` is the
|
|
49
|
+
* explicit "none" choice and paths are exactly those skills.
|
|
50
|
+
*/
|
|
51
|
+
export function configuredSkillExposure(value: unknown): ConfiguredSkillExposure {
|
|
52
|
+
if (value === undefined) return { mode: "inherit", paths: [], source: "default" };
|
|
53
|
+
const paths = normalizeSkillExposure(value, "config defaultSkills");
|
|
54
|
+
if (paths === undefined) return { mode: "inherit", paths: [], source: "config" };
|
|
55
|
+
if (paths.length === 0) return { mode: "none", paths: [], source: "config" };
|
|
56
|
+
return { mode: "explicit", paths, source: "config" };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface SkillExposureInput {
|
|
60
|
+
/**
|
|
61
|
+
* Present only on a continuation. The frozen recipe decides, including its
|
|
62
|
+
* absence: a recipe that recorded no `skills` keeps discovery instead of
|
|
63
|
+
* picking up a catalog or config choice that appeared later.
|
|
64
|
+
*/
|
|
65
|
+
saved?: { paths?: readonly string[] };
|
|
66
|
+
/** Per-launch (or batch-merged) `skills` option. */
|
|
67
|
+
requested?: unknown;
|
|
68
|
+
/** Catalog role/agent choice for this launch. */
|
|
69
|
+
catalog?: unknown;
|
|
70
|
+
/**
|
|
71
|
+
* A current-session override. Presence is the choice: `{}` means discovery,
|
|
72
|
+
* `{ value: [] }` means none, and `{ value: paths }` means exactly those.
|
|
73
|
+
* It must beat the saved config without reopening discovery.
|
|
74
|
+
*/
|
|
75
|
+
session?: { value?: unknown };
|
|
76
|
+
/** Global `defaultSkills` from the subagent config. */
|
|
77
|
+
configured?: unknown;
|
|
78
|
+
/** Workspace the child runs in; relative paths are resolved against it. */
|
|
79
|
+
cwd?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const LABELS = {
|
|
83
|
+
launch: "skills",
|
|
84
|
+
catalog: "the catalog definition's skills",
|
|
85
|
+
session: "session skills default",
|
|
86
|
+
config: "config defaultSkills",
|
|
87
|
+
} as const;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Shape-only validation: `undefined`/`null` mean "not set", and an array of
|
|
91
|
+
* non-empty strings is accepted as written. Absolutizing happens later, because
|
|
92
|
+
* the workspace a relative path belongs to is only known once it is resolved.
|
|
93
|
+
*/
|
|
94
|
+
export function normalizeSkillExposure(value: unknown, label: string): string[] | undefined {
|
|
95
|
+
if (value === undefined || value === null) return undefined;
|
|
96
|
+
if (!Array.isArray(value)) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`${label} must be an array of absolute paths to a SKILL.md file or a skill directory. ` +
|
|
99
|
+
`Omit it to inherit skill discovery, or pass [] to load no skills.`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return value.map((entry, index) => {
|
|
103
|
+
if (typeof entry !== "string" || entry.trim() === "") {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`${label}[${index}] must be a non-empty path to a SKILL.md file or a skill directory.`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return entry.trim();
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Resolve paths against the child's workspace so a frozen recipe replays the same files. */
|
|
113
|
+
export function absolutizeSkillPaths(paths: readonly string[], cwd: string | undefined): string[] {
|
|
114
|
+
return paths.map((path) => {
|
|
115
|
+
if (cwd === undefined) return isAbsolute(path) ? resolve(path) : path;
|
|
116
|
+
return isAbsolute(path) ? resolve(path) : resolve(cwd, path);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Resolve the effective exposure for an attempt.
|
|
122
|
+
*
|
|
123
|
+
* Precedence, highest first: the frozen recipe (continuation), the launch
|
|
124
|
+
* option, the catalog role/agent choice, the current-session default, the saved
|
|
125
|
+
* global config default, discovery. Every level distinguishes "not set" from
|
|
126
|
+
* `[]`; an absent session override falls through, a present one decides even
|
|
127
|
+
* when it means discovery.
|
|
128
|
+
*/
|
|
129
|
+
export function resolveSkillExposure(input: SkillExposureInput): SkillExposure {
|
|
130
|
+
if (input.saved) {
|
|
131
|
+
// A recipe written before this option existed has no key at all. That is
|
|
132
|
+
// the same "not set" the launch level uses, so it keeps discovery instead
|
|
133
|
+
// of reading as an explicit "none".
|
|
134
|
+
if (input.saved.paths === undefined) return { mode: "inherit", paths: [], source: "recipe" };
|
|
135
|
+
return exposureFrom([...input.saved.paths], "recipe");
|
|
136
|
+
}
|
|
137
|
+
for (const source of ["launch", "catalog"] as const) {
|
|
138
|
+
const value = source === "launch" ? input.requested : input.catalog;
|
|
139
|
+
const normalized = normalizeSkillExposure(value, LABELS[source]);
|
|
140
|
+
if (normalized === undefined) continue;
|
|
141
|
+
return exposureFrom(absolutizeSkillPaths(normalized, input.cwd), source);
|
|
142
|
+
}
|
|
143
|
+
if (input.session) {
|
|
144
|
+
const normalized = normalizeSkillExposure(input.session.value, LABELS.session);
|
|
145
|
+
if (normalized === undefined) return { mode: "inherit", paths: [], source: "session" };
|
|
146
|
+
return exposureFrom(absolutizeSkillPaths(normalized, input.cwd), "session");
|
|
147
|
+
}
|
|
148
|
+
const configured = normalizeSkillExposure(input.configured, LABELS.config);
|
|
149
|
+
if (configured !== undefined) return exposureFrom(absolutizeSkillPaths(configured, input.cwd), "config");
|
|
150
|
+
return { mode: "inherit", paths: [], source: "default" };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function exposureFrom(paths: readonly string[], source: SkillExposureSource): SkillExposure {
|
|
154
|
+
if (paths.length === 0) return { mode: "none", paths: [], source };
|
|
155
|
+
return { mode: "explicit", paths, source };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The child's argv for this exposure, in flag order.
|
|
160
|
+
*
|
|
161
|
+
* `inherit` emits nothing at all, which is what keeps every existing launch
|
|
162
|
+
* byte-identical. The other two states always disable discovery first; Pi merges
|
|
163
|
+
* `--skill` into that reduced set rather than re-enabling discovery.
|
|
164
|
+
*/
|
|
165
|
+
export function skillLaunchArgs(exposure: SkillExposure): string[] {
|
|
166
|
+
if (exposure.mode === "inherit") return [];
|
|
167
|
+
return ["--no-skills", ...exposure.paths.flatMap((path) => ["--skill", path])];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** One line for a launch confirmation or a continuation message. */
|
|
171
|
+
export function describeSkillExposure(exposure: SkillExposure): string {
|
|
172
|
+
if (exposure.mode === "inherit") {
|
|
173
|
+
// An ordinary launch chose nothing, so naming a source would only add
|
|
174
|
+
// noise. A session override that chose discovery is different: the user
|
|
175
|
+
// needs to see that the saved path list is not in effect.
|
|
176
|
+
const source = exposure.source === "session" ? " · source session" : "";
|
|
177
|
+
return `Skills: discovery (user catalog)${source}`;
|
|
178
|
+
}
|
|
179
|
+
const detail = exposure.mode === "none"
|
|
180
|
+
? "none (--no-skills)"
|
|
181
|
+
: `${exposure.paths.length} explicit (${exposure.paths.join(", ")})`;
|
|
182
|
+
return `Skills: ${detail} · source ${exposure.source}`;
|
|
183
|
+
}
|