proper-base 0.6.0 → 0.8.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 +270 -46
- package/UPDATES.md +129 -0
- package/index.ts +265 -136
- package/package.json +4 -3
- package/src/auto-update/availability.ts +132 -0
- package/src/auto-update/index.ts +403 -0
- package/src/auto-update/inventory.mjs +96 -0
- package/src/auto-update/readiness.ts +94 -0
- package/src/auto-update/runtime.ts +268 -0
- package/src/auto-update/settings.ts +146 -0
- package/src/autocomplete-details.ts +1 -1
- package/src/base-keybindings.ts +114 -0
- package/src/clipboard-selection.ts +310 -0
- package/src/context-tokens.ts +340 -0
- package/src/fast-mode.ts +74 -13
- package/src/footer-colors.ts +56 -3
- package/src/host-interop.ts +49 -0
- package/src/image-preview.ts +75 -66
- package/src/proactive-delegation.ts +47 -17
- package/src/prompt-display-host.ts +166 -0
- package/src/prompt-display.ts +44 -82
- package/src/prompt-sections.ts +16 -0
- package/src/session-list.ts +132 -21
- package/src/settings.ts +94 -30
- package/src/skill-context.ts +238 -159
- package/src/skills.ts +587 -0
- package/src/startup-defaults.ts +4 -122
- package/src/sticky-defaults.ts +199 -0
- package/src/transcript-cleanup.ts +8 -2
- package/src/wheel-scroll.ts +24 -27
package/src/skills.ts
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
fstatSync,
|
|
5
|
+
openSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
} from "node:fs";
|
|
9
|
+
import {
|
|
10
|
+
AgentSession,
|
|
11
|
+
type BeforeAgentStartEvent,
|
|
12
|
+
type ExtensionAPI,
|
|
13
|
+
type ExtensionContext,
|
|
14
|
+
parseSkillBlock,
|
|
15
|
+
type Skill,
|
|
16
|
+
SkillInvocationMessageComponent,
|
|
17
|
+
stripFrontmatter,
|
|
18
|
+
} from "@earendil-works/pi-coding-agent";
|
|
19
|
+
import { Container, Text } from "@earendil-works/pi-tui";
|
|
20
|
+
import { installWrapper } from "./host-interop.ts";
|
|
21
|
+
import { appendPromptSection } from "./prompt-sections.ts";
|
|
22
|
+
import {
|
|
23
|
+
readSkillContextEnabled,
|
|
24
|
+
writeSkillContextEnabled,
|
|
25
|
+
} from "./settings.ts";
|
|
26
|
+
import {
|
|
27
|
+
type ContextMessage,
|
|
28
|
+
pinSkillContext,
|
|
29
|
+
SKILL_CONTEXT_ENTRY,
|
|
30
|
+
SKILL_CONTEXT_TOOL,
|
|
31
|
+
SKILL_CONTEXT_WARNING_CHARS,
|
|
32
|
+
type SkillChange,
|
|
33
|
+
SkillContextError,
|
|
34
|
+
type SkillSelection,
|
|
35
|
+
type SkillSnapshot,
|
|
36
|
+
selectedSkills,
|
|
37
|
+
skillContextChars,
|
|
38
|
+
skillSnapshot,
|
|
39
|
+
splitSkillText,
|
|
40
|
+
} from "./skill-context.ts";
|
|
41
|
+
|
|
42
|
+
const INSTALLED = Symbol.for("proper-base.skill-context");
|
|
43
|
+
const USAGE =
|
|
44
|
+
"/skill-context [list | load <names> | remove <names> | refresh <names> | clear | on | off]";
|
|
45
|
+
const GUIDANCE = `Use skill_context to load relevant skills so their full instructions survive compaction. Explicitly selected skills remain available across follow-ups until removed or replaced, but apply only within their relevant scope. Loading reference instructions does not authorize unrelated actions. A fresh workflow request is new work even when its instructions are already loaded; restoration is never a request to repeat completed work. Keep track of completed steps in your ordinary task progress. Follow the user's explicit priorities and the instruction hierarchy; identify material skill conflicts rather than using mention order as precedence. Use only the smallest relevant set of automatically selected skills. /skill-context lists, removes and refreshes selections. Skill permissions are unchanged.`;
|
|
46
|
+
|
|
47
|
+
/** Exact catalog matches only. The first non-command starts literal task text. */
|
|
48
|
+
export function parseSkillCommands(
|
|
49
|
+
text: string,
|
|
50
|
+
available: ReadonlySet<string>,
|
|
51
|
+
): { names: string[]; request: string } | undefined {
|
|
52
|
+
const names: string[] = [];
|
|
53
|
+
let request = text;
|
|
54
|
+
while (request.startsWith("/skill:")) {
|
|
55
|
+
const match = /^\/skill:([^\s]+)(?:\s+|$)/u.exec(request);
|
|
56
|
+
if (!match?.[1] || !available.has(match[1])) break;
|
|
57
|
+
names.push(match[1]);
|
|
58
|
+
request = request.slice(match[0].length);
|
|
59
|
+
}
|
|
60
|
+
if (!names.length) return undefined;
|
|
61
|
+
if (/^--(?:\s|$)/.test(request)) request = request.slice(2).trimStart();
|
|
62
|
+
return { names, request };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function filesystemError(error: unknown): boolean {
|
|
66
|
+
return (
|
|
67
|
+
error instanceof Error &&
|
|
68
|
+
typeof (error as NodeJS.ErrnoException).code === "string"
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Read a discovered file, never a user-supplied arbitrary path or executable template. */
|
|
73
|
+
export function loadSkillSnapshot(skill: Skill): SkillSnapshot {
|
|
74
|
+
let file: number | undefined;
|
|
75
|
+
try {
|
|
76
|
+
const location = realpathSync(skill.filePath);
|
|
77
|
+
file = openSync(location, "r");
|
|
78
|
+
const stat = fstatSync(file);
|
|
79
|
+
if (!stat.isFile())
|
|
80
|
+
throw new SkillContextError(`Skill ${skill.name} is not a regular file.`);
|
|
81
|
+
const body = stripFrontmatter(readFileSync(file, "utf8")).trim();
|
|
82
|
+
const baseDir = skill.baseDir;
|
|
83
|
+
if (
|
|
84
|
+
[skill.name, location, baseDir].some((value) =>
|
|
85
|
+
/[<>"\p{Cc}]/u.test(value),
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
throw new SkillContextError(
|
|
89
|
+
`Skill ${skill.name} has a name or path that cannot be represented in a Pi skill block.`,
|
|
90
|
+
);
|
|
91
|
+
const block = `<skill name="${skill.name}" location="${location}">\nReferences are relative to ${baseDir}.\n\n${body}\n</skill>`;
|
|
92
|
+
const snapshot = skillSnapshot(block);
|
|
93
|
+
if (!snapshot)
|
|
94
|
+
throw new SkillContextError(
|
|
95
|
+
`Skill ${skill.name} contains a reserved skill-block delimiter.`,
|
|
96
|
+
);
|
|
97
|
+
return snapshot;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (filesystemError(error))
|
|
100
|
+
throw new SkillContextError(
|
|
101
|
+
`Cannot read skill ${skill.name}: ${(error as NodeJS.ErrnoException).code}.`,
|
|
102
|
+
);
|
|
103
|
+
throw error;
|
|
104
|
+
} finally {
|
|
105
|
+
if (file !== undefined) closeSync(file);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function findSkill(catalog: Skill[], name: string): Skill {
|
|
110
|
+
const matches = catalog.filter((skill) => skill.name === name);
|
|
111
|
+
if (matches.length !== 1)
|
|
112
|
+
throw new SkillContextError(
|
|
113
|
+
matches.length
|
|
114
|
+
? `Skill name ${name} is ambiguous.`
|
|
115
|
+
: `Unknown skill: ${name}.`,
|
|
116
|
+
);
|
|
117
|
+
return matches[0] as Skill;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function findSelection(
|
|
121
|
+
selections: SkillSelection[],
|
|
122
|
+
name: string,
|
|
123
|
+
): SkillSelection {
|
|
124
|
+
const matches = selections.filter(
|
|
125
|
+
(skill) => skill.name === name || skill.location === name,
|
|
126
|
+
);
|
|
127
|
+
if (matches.length !== 1)
|
|
128
|
+
throw new SkillContextError(
|
|
129
|
+
matches.length
|
|
130
|
+
? `Skill name ${name} is ambiguous; use its full path.`
|
|
131
|
+
: `No selected snapshot for ${name}.`,
|
|
132
|
+
);
|
|
133
|
+
return matches[0] as SkillSelection;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function mergedSelection(
|
|
137
|
+
selections: SkillSelection[],
|
|
138
|
+
incoming: SkillSnapshot[],
|
|
139
|
+
): SkillSnapshot[] {
|
|
140
|
+
const active = new Map(
|
|
141
|
+
selections
|
|
142
|
+
.filter((skill) => skill.selected)
|
|
143
|
+
.map((skill) => [skill.location, skill]),
|
|
144
|
+
);
|
|
145
|
+
const merged = new Map<string, SkillSnapshot>(active);
|
|
146
|
+
for (const skill of incoming) merged.set(skill.location, skill);
|
|
147
|
+
return [...merged.values()];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type LiveSession = Pick<AgentSession, "resourceLoader" | "sessionManager">;
|
|
151
|
+
|
|
152
|
+
/** One activation path for commands and tools, using Pi's discovered catalog. */
|
|
153
|
+
export function registerSkillContext(
|
|
154
|
+
pi: ExtensionAPI,
|
|
155
|
+
agentDir: string,
|
|
156
|
+
getSession: (ctx: ExtensionContext) => unknown,
|
|
157
|
+
) {
|
|
158
|
+
let context: ExtensionContext | undefined;
|
|
159
|
+
let disposed = false;
|
|
160
|
+
let runEnabled: boolean | undefined;
|
|
161
|
+
let approvedSelection: string | undefined;
|
|
162
|
+
const dispatch = new AsyncLocalStorage<{
|
|
163
|
+
expand: boolean;
|
|
164
|
+
prepared?: { text: string; expanded: string };
|
|
165
|
+
}>();
|
|
166
|
+
const enabled = () => !disposed && readSkillContextEnabled(agentDir);
|
|
167
|
+
const effective = () => !disposed && (runEnabled ?? enabled());
|
|
168
|
+
let toolHidden = false;
|
|
169
|
+
const syncTool = (active: boolean) => {
|
|
170
|
+
const tools = pi.getActiveTools?.();
|
|
171
|
+
if (!tools || !pi.setActiveTools) return;
|
|
172
|
+
if (!active && tools.includes(SKILL_CONTEXT_TOOL)) {
|
|
173
|
+
pi.setActiveTools(tools.filter((name) => name !== SKILL_CONTEXT_TOOL));
|
|
174
|
+
toolHidden = true;
|
|
175
|
+
} else if (active && toolHidden) {
|
|
176
|
+
pi.setActiveTools([...new Set([...tools, SKILL_CONTEXT_TOOL])]);
|
|
177
|
+
toolHidden = false;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const state = (ctx: ExtensionContext) =>
|
|
181
|
+
selectedSkills(ctx.sessionManager.getBranch());
|
|
182
|
+
const catalog = (ctx: ExtensionContext): Skill[] => {
|
|
183
|
+
const session = getSession(ctx) as LiveSession | undefined;
|
|
184
|
+
if (!session?.resourceLoader?.getSkills)
|
|
185
|
+
throw new SkillContextError(
|
|
186
|
+
"This Pi runtime cannot expose its skill catalog to proper-base. Disable Skill context management to use native skills.",
|
|
187
|
+
);
|
|
188
|
+
return session.resourceLoader.getSkills().skills;
|
|
189
|
+
};
|
|
190
|
+
const save = (change: SkillChange) =>
|
|
191
|
+
pi.appendEntry(SKILL_CONTEXT_ENTRY, change);
|
|
192
|
+
const requireEnabled = (running = false) => {
|
|
193
|
+
if (!(running ? effective() : enabled()))
|
|
194
|
+
throw new SkillContextError(
|
|
195
|
+
"Skill context management is disabled. Use /skill-context on or /settings to enable it.",
|
|
196
|
+
);
|
|
197
|
+
};
|
|
198
|
+
const load = (
|
|
199
|
+
ctx: ExtensionContext,
|
|
200
|
+
names: string[],
|
|
201
|
+
automatic: boolean,
|
|
202
|
+
): SkillSnapshot[] => {
|
|
203
|
+
if (!names.length)
|
|
204
|
+
throw new SkillContextError("Specify at least one skill name.");
|
|
205
|
+
const available = catalog(ctx);
|
|
206
|
+
const selections = state(ctx);
|
|
207
|
+
const loaded = new Map<string, SkillSnapshot>();
|
|
208
|
+
for (const name of names) {
|
|
209
|
+
const skill = findSkill(available, name);
|
|
210
|
+
if (automatic && skill.disableModelInvocation)
|
|
211
|
+
throw new SkillContextError(
|
|
212
|
+
`Skill ${name} requires explicit user invocation.`,
|
|
213
|
+
);
|
|
214
|
+
const snapshot = loadSkillSnapshot(skill);
|
|
215
|
+
const existing = selections.find(
|
|
216
|
+
(item) => item.location === snapshot.location && item.selected,
|
|
217
|
+
);
|
|
218
|
+
// A model cannot silently refresh an already selected instruction version.
|
|
219
|
+
loaded.set(
|
|
220
|
+
snapshot.location,
|
|
221
|
+
automatic && existing ? existing : snapshot,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return [...loaded.values()];
|
|
225
|
+
};
|
|
226
|
+
const confirmSelection = async (
|
|
227
|
+
ctx: ExtensionContext,
|
|
228
|
+
incoming: SkillSnapshot[] = [],
|
|
229
|
+
signal = ctx.signal,
|
|
230
|
+
): Promise<boolean> => {
|
|
231
|
+
const skills = mergedSelection(state(ctx), incoming);
|
|
232
|
+
const chars = skillContextChars(skills);
|
|
233
|
+
if (chars <= SKILL_CONTEXT_WARNING_CHARS) return true;
|
|
234
|
+
const key = skills
|
|
235
|
+
.map((skill) => skill.hash)
|
|
236
|
+
.sort()
|
|
237
|
+
.join(":");
|
|
238
|
+
if (key === approvedSelection) return true;
|
|
239
|
+
if (!ctx.hasUI)
|
|
240
|
+
throw new SkillContextError(
|
|
241
|
+
`Selected skill instructions total ${chars} characters, above the ${SKILL_CONTEXT_WARNING_CHARS}-character warning threshold. Continuing requires confirmation through an interactive or RPC UI. No instructions were truncated.`,
|
|
242
|
+
);
|
|
243
|
+
const confirmed = await ctx.ui.confirm(
|
|
244
|
+
"Large skill selection",
|
|
245
|
+
`Selected skill instructions total ${chars} characters, above the ${SKILL_CONTEXT_WARNING_CHARS}-character warning threshold. This may leave less room for conversation and tool output. Continue with all selected skills?`,
|
|
246
|
+
signal ? { signal } : undefined,
|
|
247
|
+
);
|
|
248
|
+
signal?.throwIfAborted();
|
|
249
|
+
if (
|
|
250
|
+
!confirmed ||
|
|
251
|
+
disposed ||
|
|
252
|
+
context?.sessionManager !== ctx.sessionManager
|
|
253
|
+
)
|
|
254
|
+
return false;
|
|
255
|
+
approvedSelection = key;
|
|
256
|
+
return true;
|
|
257
|
+
};
|
|
258
|
+
const status = (ctx: ExtensionContext): string => {
|
|
259
|
+
const selections = state(ctx);
|
|
260
|
+
const rows = selections.map((selection) => {
|
|
261
|
+
let version = "snapshot retained";
|
|
262
|
+
try {
|
|
263
|
+
const skill = catalog(ctx).find((item) => item.name === selection.name);
|
|
264
|
+
version = !skill
|
|
265
|
+
? "not in current catalog"
|
|
266
|
+
: loadSkillSnapshot(skill).hash === selection.hash
|
|
267
|
+
? "current"
|
|
268
|
+
: "changed on disk; refresh explicitly";
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (!(error instanceof SkillContextError)) throw error;
|
|
271
|
+
version = "source unavailable; snapshot retained";
|
|
272
|
+
}
|
|
273
|
+
return `${selection.selected ? "selected" : "removed"}: ${selection.name} [${selection.hash.slice(0, 12)}; ${version}; ${selection.source}]\n ${selection.location}`;
|
|
274
|
+
});
|
|
275
|
+
const active = selections.filter((skill) => skill.selected);
|
|
276
|
+
const chars = skillContextChars(active);
|
|
277
|
+
return `Skill context management: ${enabled() ? "enabled" : "disabled"}\n${active.length} selected; ${chars} instruction characters; confirmation above ${SKILL_CONTEXT_WARNING_CHARS}.\n${rows.join("\n") || "No skill snapshots in this branch."}\n${USAGE}`;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// Preflight after all input transforms, then expand the exact approved
|
|
281
|
+
// snapshot at Pi's synchronous boundary. Dispatch scope preserves opt-outs.
|
|
282
|
+
const prototype = AgentSession.prototype;
|
|
283
|
+
const owner = prototype as typeof prototype & { [INSTALLED]?: () => void };
|
|
284
|
+
owner[INSTALLED]?.();
|
|
285
|
+
const native = Reflect.get(prototype, "_expandSkillCommand");
|
|
286
|
+
const nativeInput = Reflect.get(prototype, "_runInputHandlers");
|
|
287
|
+
const remove =
|
|
288
|
+
typeof native === "function" && typeof nativeInput === "function"
|
|
289
|
+
? installWrapper(
|
|
290
|
+
prototype,
|
|
291
|
+
"_expandSkillCommand",
|
|
292
|
+
function (this: AgentSession, text: string) {
|
|
293
|
+
if (
|
|
294
|
+
!context ||
|
|
295
|
+
this.sessionManager !== context.sessionManager ||
|
|
296
|
+
!(this.isStreaming ? effective() : enabled())
|
|
297
|
+
)
|
|
298
|
+
return Reflect.apply(native, this, [text]);
|
|
299
|
+
if (!text.startsWith("/skill:"))
|
|
300
|
+
return Reflect.apply(native, this, [text]);
|
|
301
|
+
const prepared = dispatch.getStore()?.prepared;
|
|
302
|
+
if (prepared?.text === text) return prepared.expanded;
|
|
303
|
+
const parsed = parseSkillCommands(
|
|
304
|
+
text,
|
|
305
|
+
new Set(catalog(context).map((skill) => skill.name)),
|
|
306
|
+
);
|
|
307
|
+
if (!parsed) return Reflect.apply(native, this, [text]);
|
|
308
|
+
const snapshots = load(context, parsed.names, false);
|
|
309
|
+
return [
|
|
310
|
+
...snapshots.map((skill) => skill.block),
|
|
311
|
+
...(parsed.request ? [parsed.request] : []),
|
|
312
|
+
].join("\n\n");
|
|
313
|
+
},
|
|
314
|
+
)
|
|
315
|
+
: undefined;
|
|
316
|
+
const restorers = remove ? [remove] : [];
|
|
317
|
+
if (remove) {
|
|
318
|
+
for (const name of ["prompt", "steer", "followUp"] as const) {
|
|
319
|
+
const original = prototype[name];
|
|
320
|
+
restorers.push(
|
|
321
|
+
installWrapper(
|
|
322
|
+
prototype,
|
|
323
|
+
name,
|
|
324
|
+
function (this: AgentSession, ...args: unknown[]) {
|
|
325
|
+
if (disposed || this.sessionManager !== context?.sessionManager)
|
|
326
|
+
return Reflect.apply(original, this, args);
|
|
327
|
+
const options = args[1] as
|
|
328
|
+
| { expandPromptTemplates?: boolean }
|
|
329
|
+
| undefined;
|
|
330
|
+
return dispatch.run(
|
|
331
|
+
{
|
|
332
|
+
expand:
|
|
333
|
+
name !== "prompt" || options?.expandPromptTemplates !== false,
|
|
334
|
+
},
|
|
335
|
+
() => Reflect.apply(original, this, args),
|
|
336
|
+
);
|
|
337
|
+
},
|
|
338
|
+
),
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
restorers.push(
|
|
342
|
+
installWrapper(
|
|
343
|
+
prototype,
|
|
344
|
+
"_runInputHandlers",
|
|
345
|
+
async function (this: AgentSession, ...args: unknown[]) {
|
|
346
|
+
const input = (await Reflect.apply(nativeInput, this, args)) as
|
|
347
|
+
| { text: string }
|
|
348
|
+
| undefined;
|
|
349
|
+
const ctx = context;
|
|
350
|
+
const scope = dispatch.getStore();
|
|
351
|
+
if (
|
|
352
|
+
!input ||
|
|
353
|
+
!scope ||
|
|
354
|
+
!ctx ||
|
|
355
|
+
this.sessionManager !== ctx.sessionManager ||
|
|
356
|
+
!(this.isStreaming ? effective() : enabled())
|
|
357
|
+
)
|
|
358
|
+
return input;
|
|
359
|
+
let snapshots = splitSkillText(input.text).skills;
|
|
360
|
+
if (scope.expand && input.text.startsWith("/skill:")) {
|
|
361
|
+
const parsed = parseSkillCommands(
|
|
362
|
+
input.text,
|
|
363
|
+
new Set(catalog(ctx).map((skill) => skill.name)),
|
|
364
|
+
);
|
|
365
|
+
if (parsed) {
|
|
366
|
+
snapshots = load(ctx, parsed.names, false);
|
|
367
|
+
scope.prepared = {
|
|
368
|
+
text: input.text,
|
|
369
|
+
expanded: [
|
|
370
|
+
...snapshots.map((skill) => skill.block),
|
|
371
|
+
...(parsed.request ? [parsed.request] : []),
|
|
372
|
+
].join("\n\n"),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return (await confirmSelection(ctx, snapshots)) ? input : undefined;
|
|
377
|
+
},
|
|
378
|
+
),
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
const stop = () => {
|
|
382
|
+
disposed = true;
|
|
383
|
+
context = undefined;
|
|
384
|
+
for (const restore of restorers.reverse()) restore();
|
|
385
|
+
dispatch.disable();
|
|
386
|
+
if (owner[INSTALLED] === stop) delete owner[INSTALLED];
|
|
387
|
+
};
|
|
388
|
+
owner[INSTALLED] = stop;
|
|
389
|
+
|
|
390
|
+
pi.registerCommand?.("skill-context", {
|
|
391
|
+
description:
|
|
392
|
+
"List, load, remove or refresh skill snapshots; enable/disable skill context management",
|
|
393
|
+
getArgumentCompletions: (prefix) => {
|
|
394
|
+
const values = [
|
|
395
|
+
"list",
|
|
396
|
+
"load",
|
|
397
|
+
"remove",
|
|
398
|
+
"refresh",
|
|
399
|
+
"clear",
|
|
400
|
+
"on",
|
|
401
|
+
"off",
|
|
402
|
+
];
|
|
403
|
+
return values
|
|
404
|
+
.filter((value) => value.startsWith(prefix))
|
|
405
|
+
.map((value) => ({ value, label: value }));
|
|
406
|
+
},
|
|
407
|
+
handler: async (args, ctx) => {
|
|
408
|
+
const [action = "list", ...names] = args
|
|
409
|
+
.trim()
|
|
410
|
+
.split(/\s+/)
|
|
411
|
+
.filter(Boolean);
|
|
412
|
+
try {
|
|
413
|
+
if (action === "list") {
|
|
414
|
+
ctx.ui.notify(status(ctx), "info");
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (!ctx.isIdle() || ctx.hasPendingMessages())
|
|
418
|
+
throw new SkillContextError(
|
|
419
|
+
"Wait for the current run and queued prompts before changing skill selections.",
|
|
420
|
+
);
|
|
421
|
+
if ((action === "on" || action === "off") && !names.length) {
|
|
422
|
+
writeSkillContextEnabled(agentDir, action === "on");
|
|
423
|
+
syncTool(action === "on");
|
|
424
|
+
ctx.ui.notify(
|
|
425
|
+
`Skill context management ${action === "on" ? "enabled" : "disabled"}. Applies to the next run; native skills remain available.`,
|
|
426
|
+
"info",
|
|
427
|
+
);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (action === "clear" && !names.length) save({ action: "clear" });
|
|
431
|
+
else if (action === "remove" && names.length) {
|
|
432
|
+
const selections = state(ctx);
|
|
433
|
+
save({
|
|
434
|
+
action: "remove",
|
|
435
|
+
locations: names.map(
|
|
436
|
+
(name) => findSelection(selections, name).location,
|
|
437
|
+
),
|
|
438
|
+
});
|
|
439
|
+
} else if (
|
|
440
|
+
(action === "load" || action === "refresh") &&
|
|
441
|
+
names.length
|
|
442
|
+
) {
|
|
443
|
+
requireEnabled();
|
|
444
|
+
const requested =
|
|
445
|
+
action === "refresh"
|
|
446
|
+
? names.map((name) => findSelection(state(ctx), name).name)
|
|
447
|
+
: names;
|
|
448
|
+
const snapshots = load(ctx, requested, false);
|
|
449
|
+
if (!(await confirmSelection(ctx, snapshots))) return;
|
|
450
|
+
save({
|
|
451
|
+
action: "load",
|
|
452
|
+
blocks: snapshots.map((skill) => skill.block),
|
|
453
|
+
});
|
|
454
|
+
} else throw new SkillContextError(USAGE);
|
|
455
|
+
ctx.ui.notify(status(ctx), "info");
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (
|
|
458
|
+
!(error instanceof SkillContextError) &&
|
|
459
|
+
!filesystemError(error) &&
|
|
460
|
+
!(error instanceof SyntaxError)
|
|
461
|
+
)
|
|
462
|
+
throw error;
|
|
463
|
+
ctx.ui.notify(
|
|
464
|
+
error instanceof Error ? error.message : String(error),
|
|
465
|
+
"error",
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
pi.registerTool?.({
|
|
472
|
+
name: SKILL_CONTEXT_TOOL,
|
|
473
|
+
label: "Skill context",
|
|
474
|
+
description:
|
|
475
|
+
"Load full instructions for relevant skills from Pi's catalog, or list selected snapshots. Use this instead of reading SKILL.md to preserve instructions across compaction. Loading supplies guidance, not permission to repeat prior actions. Explicit-only skills require the user's /skill command.",
|
|
476
|
+
parameters: {
|
|
477
|
+
type: "object",
|
|
478
|
+
properties: {
|
|
479
|
+
action: { type: "string", enum: ["list", "load"] },
|
|
480
|
+
names: { type: "array", items: { type: "string" }, minItems: 1 },
|
|
481
|
+
},
|
|
482
|
+
required: ["action"],
|
|
483
|
+
additionalProperties: false,
|
|
484
|
+
} as const,
|
|
485
|
+
executionMode: "sequential",
|
|
486
|
+
renderResult(result, options) {
|
|
487
|
+
const text = result.content
|
|
488
|
+
.filter((part) => part.type === "text")
|
|
489
|
+
.map((part) => part.text)
|
|
490
|
+
.join("\n\n");
|
|
491
|
+
const { skills, request } = splitSkillText(text);
|
|
492
|
+
const container = new Container();
|
|
493
|
+
for (const skill of skills) {
|
|
494
|
+
const parsed = parseSkillBlock(skill.block);
|
|
495
|
+
if (!parsed) continue;
|
|
496
|
+
const component = new SkillInvocationMessageComponent(parsed);
|
|
497
|
+
component.setExpanded(options.expanded);
|
|
498
|
+
container.addChild(component);
|
|
499
|
+
}
|
|
500
|
+
if (request) container.addChild(new Text(request, 0, 0));
|
|
501
|
+
return container;
|
|
502
|
+
},
|
|
503
|
+
async execute(_id, params, signal, _update, ctx) {
|
|
504
|
+
requireEnabled(true);
|
|
505
|
+
signal?.throwIfAborted();
|
|
506
|
+
const input = params as { action?: unknown; names?: unknown } | null;
|
|
507
|
+
if (input?.action === "list")
|
|
508
|
+
return {
|
|
509
|
+
content: [{ type: "text", text: status(ctx) }],
|
|
510
|
+
details: undefined,
|
|
511
|
+
};
|
|
512
|
+
if (
|
|
513
|
+
input?.action !== "load" ||
|
|
514
|
+
!Array.isArray(input.names) ||
|
|
515
|
+
!input.names.every((name): name is string => typeof name === "string")
|
|
516
|
+
)
|
|
517
|
+
throw new SkillContextError(
|
|
518
|
+
"Expected action load with an array of skill names.",
|
|
519
|
+
);
|
|
520
|
+
const snapshots = load(ctx, input.names, true);
|
|
521
|
+
if (!(await confirmSelection(ctx, snapshots, signal)))
|
|
522
|
+
throw new SkillContextError(
|
|
523
|
+
"Large skill selection cancelled by the user. No new skills were loaded.",
|
|
524
|
+
);
|
|
525
|
+
return {
|
|
526
|
+
content: [
|
|
527
|
+
{
|
|
528
|
+
type: "text",
|
|
529
|
+
text: snapshots.map((skill) => skill.block).join("\n\n"),
|
|
530
|
+
},
|
|
531
|
+
],
|
|
532
|
+
details: {
|
|
533
|
+
skills: snapshots.map(({ name, location, hash }) => ({
|
|
534
|
+
name,
|
|
535
|
+
location,
|
|
536
|
+
hash,
|
|
537
|
+
})),
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
return {
|
|
544
|
+
start(ctx: ExtensionContext) {
|
|
545
|
+
context = ctx;
|
|
546
|
+
runEnabled = undefined;
|
|
547
|
+
approvedSelection = undefined;
|
|
548
|
+
syncTool(enabled());
|
|
549
|
+
if (!remove && enabled())
|
|
550
|
+
ctx.ui.notify(
|
|
551
|
+
"This Pi version cannot preflight managed skill invocations. Use /skill-context load or disable Skill context management.",
|
|
552
|
+
"warning",
|
|
553
|
+
);
|
|
554
|
+
},
|
|
555
|
+
stop,
|
|
556
|
+
prepare(event: BeforeAgentStartEvent) {
|
|
557
|
+
runEnabled = enabled();
|
|
558
|
+
syncTool(runEnabled);
|
|
559
|
+
if (runEnabled)
|
|
560
|
+
appendPromptSection(
|
|
561
|
+
event.systemPromptOptions,
|
|
562
|
+
"proper_base_skills",
|
|
563
|
+
GUIDANCE,
|
|
564
|
+
);
|
|
565
|
+
},
|
|
566
|
+
async transform<T extends ContextMessage>(
|
|
567
|
+
messages: T[],
|
|
568
|
+
ctx: ExtensionContext,
|
|
569
|
+
): Promise<T[]> {
|
|
570
|
+
if (!effective()) return messages;
|
|
571
|
+
try {
|
|
572
|
+
if (!(await confirmSelection(ctx)))
|
|
573
|
+
throw new SkillContextError(
|
|
574
|
+
"Large skill selection cancelled by the user. Remove skills with /skill-context remove <name> or /skill-context clear, or retry to confirm.",
|
|
575
|
+
);
|
|
576
|
+
return pinSkillContext(messages, ctx.sessionManager.getBranch());
|
|
577
|
+
} catch (error) {
|
|
578
|
+
if (!(error instanceof SkillContextError)) throw error;
|
|
579
|
+
ctx.ui.notify(error.message, "error");
|
|
580
|
+
// Context handlers' exceptions alone are advisory in Pi. Abort the
|
|
581
|
+
// run as well, so unconfirmed instructions cannot reach the model.
|
|
582
|
+
ctx.abort();
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
};
|
|
587
|
+
}
|