@xynogen/pix-skills 0.7.3 → 0.7.5
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/package.json +3 -3
- package/src/index.test.ts +38 -1
- package/src/index.ts +11 -3
- package/src/remote.test.ts +43 -0
- package/src/remote.ts +102 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-skills",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.5",
|
|
4
4
|
"description": "Pi extension — agent skill loader (skill tool + skills bundle)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@xynogen/pix-data": "^0.4.0",
|
|
50
|
-
"@xynogen/pix-gate": "^0.
|
|
50
|
+
"@xynogen/pix-gate": "^0.2.0",
|
|
51
51
|
"@xynogen/pix-pretty": "^1.7.20",
|
|
52
|
-
"@xynogen/pix-runtime": "^0.
|
|
52
|
+
"@xynogen/pix-runtime": "^0.4.0"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/src/index.test.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it } from "bun:test";
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
|
|
2
2
|
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
+
import { pixRuntime } from "@xynogen/pix-runtime/config";
|
|
6
|
+
import { ioSection } from "@xynogen/pix-runtime/sections";
|
|
5
7
|
|
|
6
8
|
import registerSkills, {
|
|
7
9
|
copySkillResource,
|
|
@@ -164,6 +166,41 @@ describe("formatCollapsedSkillResult", () => {
|
|
|
164
166
|
});
|
|
165
167
|
});
|
|
166
168
|
|
|
169
|
+
describe("remote skill timeout config", () => {
|
|
170
|
+
beforeEach(async () => {
|
|
171
|
+
await pixRuntime().update(ioSection, { timeoutSec: 30 });
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("passes the configured timeout and tool cancellation signal to fetch", async () => {
|
|
175
|
+
await pixRuntime().update(ioSection, { timeoutSec: 120 });
|
|
176
|
+
const controller = new AbortController();
|
|
177
|
+
let seenRequest: RequestInit | undefined;
|
|
178
|
+
const originalFetch = globalThis.fetch;
|
|
179
|
+
globalThis.fetch = mock((_input: string | URL | Request, init?: RequestInit) => {
|
|
180
|
+
seenRequest = init;
|
|
181
|
+
return Promise.resolve(new Response(JSON.stringify({ skills: [] }), { status: 200 }));
|
|
182
|
+
}) as unknown as typeof fetch;
|
|
183
|
+
try {
|
|
184
|
+
let registered: Record<string, unknown> = {};
|
|
185
|
+
registerSkills({
|
|
186
|
+
registerTool(tool: unknown) {
|
|
187
|
+
registered = tool as Record<string, unknown>;
|
|
188
|
+
},
|
|
189
|
+
on() {},
|
|
190
|
+
} as never);
|
|
191
|
+
const execute = registered.execute as (...args: unknown[]) => Promise<unknown>;
|
|
192
|
+
await execute("call", { search: "typescript" }, controller.signal, undefined, {});
|
|
193
|
+
const requestSignal = seenRequest?.signal;
|
|
194
|
+
expect(requestSignal).toBeDefined();
|
|
195
|
+
expect(requestSignal).not.toBe(controller.signal);
|
|
196
|
+
expect(requestSignal?.aborted).toBe(false);
|
|
197
|
+
} finally {
|
|
198
|
+
globalThis.fetch = originalFetch;
|
|
199
|
+
await pixRuntime().update(ioSection, { timeoutSec: 30 });
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
167
204
|
describe("skill renderer", () => {
|
|
168
205
|
it("uses the self-rendered shell so the compact status mark has no box padding", () => {
|
|
169
206
|
let registered: Record<string, unknown> = {};
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
20
20
|
import { Text } from "@earendil-works/pi-tui";
|
|
21
21
|
import { formatCollapsedToolRow, hideCollapsedToolCall } from "@xynogen/pix-pretty/utils";
|
|
22
22
|
import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
|
|
23
|
+
import { ioTimeoutMs } from "@xynogen/pix-runtime/io";
|
|
23
24
|
import { once } from "@xynogen/pix-runtime/once";
|
|
24
25
|
import { Type } from "typebox";
|
|
25
26
|
import {
|
|
@@ -487,7 +488,7 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
487
488
|
executionMode: "sequential",
|
|
488
489
|
parameters: ParamsSchema,
|
|
489
490
|
|
|
490
|
-
async execute(_toolCallId, params,
|
|
491
|
+
async execute(_toolCallId, params, signal, _upd, toolCtx) {
|
|
491
492
|
const ok = (text: string, details: SkillResultDetails) => ({
|
|
492
493
|
content: [{ type: "text" as const, text }],
|
|
493
494
|
details,
|
|
@@ -510,7 +511,10 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
510
511
|
|
|
511
512
|
if (search) {
|
|
512
513
|
try {
|
|
513
|
-
const results = await searchRemoteSkills(search
|
|
514
|
+
const results = await searchRemoteSkills(search, fetch, {
|
|
515
|
+
signal,
|
|
516
|
+
timeoutMs: ioTimeoutMs(),
|
|
517
|
+
});
|
|
514
518
|
return ok(formatRemoteSkillSearch(search, results), {
|
|
515
519
|
mode: "search",
|
|
516
520
|
query: search,
|
|
@@ -542,7 +546,11 @@ function registerSkillLoader(pi: ExtensionAPI): void {
|
|
|
542
546
|
let remoteCached: boolean | undefined;
|
|
543
547
|
if (source) {
|
|
544
548
|
try {
|
|
545
|
-
const remote = await fetchRemoteSkill(source, name, {
|
|
549
|
+
const remote = await fetchRemoteSkill(source, name, {
|
|
550
|
+
refresh,
|
|
551
|
+
signal,
|
|
552
|
+
timeoutMs: ioTimeoutMs(),
|
|
553
|
+
});
|
|
546
554
|
entry = { name: remote.name, path: remote.path, root: remote.root };
|
|
547
555
|
remoteSource = remote.source;
|
|
548
556
|
remoteCached = remote.cached;
|
package/src/remote.test.ts
CHANGED
|
@@ -50,6 +50,23 @@ describe("Skills.sh search", () => {
|
|
|
50
50
|
it("rejects undersized queries", async () => {
|
|
51
51
|
expect(searchRemoteSkills("x")).rejects.toThrow("between 2 and 200");
|
|
52
52
|
});
|
|
53
|
+
|
|
54
|
+
it("cancels an in-flight search", async () => {
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
const fetcher = ((_input: string | URL | Request, init?: RequestInit) =>
|
|
57
|
+
new Promise<Response>((_resolve, reject) => {
|
|
58
|
+
init?.signal?.addEventListener(
|
|
59
|
+
"abort",
|
|
60
|
+
() => reject(init.signal?.reason ?? new DOMException("Aborted", "AbortError")),
|
|
61
|
+
{ once: true },
|
|
62
|
+
);
|
|
63
|
+
})) as typeof fetch;
|
|
64
|
+
|
|
65
|
+
const pending = searchRemoteSkills("hallmark", fetcher, { signal: controller.signal });
|
|
66
|
+
controller.abort();
|
|
67
|
+
|
|
68
|
+
await expect(pending).rejects.toThrow("Remote request cancelled");
|
|
69
|
+
});
|
|
53
70
|
});
|
|
54
71
|
|
|
55
72
|
describe("remote skill fetching", () => {
|
|
@@ -112,6 +129,32 @@ describe("remote skill fetching", () => {
|
|
|
112
129
|
expect(calls).toHaveLength(callCount);
|
|
113
130
|
});
|
|
114
131
|
|
|
132
|
+
it("times out a stalled remote fetch", async () => {
|
|
133
|
+
const root = await mkdtemp(join(tmpdir(), "pix-remote-skill-"));
|
|
134
|
+
roots.push(root);
|
|
135
|
+
let aborted = false;
|
|
136
|
+
const fetcher = ((_input: string | URL | Request, init?: RequestInit) =>
|
|
137
|
+
new Promise<Response>((_resolve, reject) => {
|
|
138
|
+
init?.signal?.addEventListener(
|
|
139
|
+
"abort",
|
|
140
|
+
() => {
|
|
141
|
+
aborted = true;
|
|
142
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
143
|
+
},
|
|
144
|
+
{ once: true },
|
|
145
|
+
);
|
|
146
|
+
})) as typeof fetch;
|
|
147
|
+
|
|
148
|
+
await expect(
|
|
149
|
+
fetchRemoteSkill("owner/repo", "hallmark", {
|
|
150
|
+
cacheRoot: root,
|
|
151
|
+
fetcher,
|
|
152
|
+
timeoutMs: 10,
|
|
153
|
+
}),
|
|
154
|
+
).rejects.toThrow("Remote request timed out after 10 ms");
|
|
155
|
+
expect(aborted).toBe(true);
|
|
156
|
+
});
|
|
157
|
+
|
|
115
158
|
it("requires the selected skill name to match frontmatter", async () => {
|
|
116
159
|
const root = await mkdtemp(join(tmpdir(), "pix-remote-skill-"));
|
|
117
160
|
roots.push(root);
|
package/src/remote.ts
CHANGED
|
@@ -28,6 +28,45 @@ export interface RemoteSkillEntry {
|
|
|
28
28
|
|
|
29
29
|
type FetchLike = typeof fetch;
|
|
30
30
|
|
|
31
|
+
type RemoteRequestOptions = {
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
timeoutMs?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
async function withRemoteDeadline<T>(
|
|
37
|
+
options: RemoteRequestOptions,
|
|
38
|
+
operation: (signal: AbortSignal) => Promise<T>,
|
|
39
|
+
): Promise<T> {
|
|
40
|
+
const timeoutMs = options.timeoutMs ?? 30_000;
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
let timedOut = false;
|
|
43
|
+
const cancel = () => controller.abort(options.signal?.reason);
|
|
44
|
+
|
|
45
|
+
if (options.signal?.aborted) cancel();
|
|
46
|
+
else options.signal?.addEventListener("abort", cancel, { once: true });
|
|
47
|
+
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
timedOut = true;
|
|
50
|
+
controller.abort();
|
|
51
|
+
}, timeoutMs);
|
|
52
|
+
timer.unref?.();
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
if (controller.signal.aborted) throw new Error("Remote request cancelled");
|
|
56
|
+
return await operation(controller.signal);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (timedOut) {
|
|
59
|
+
const duration = timeoutMs % 1000 === 0 ? `${timeoutMs / 1000} seconds` : `${timeoutMs} ms`;
|
|
60
|
+
throw new Error(`Remote request timed out after ${duration}`);
|
|
61
|
+
}
|
|
62
|
+
if (options.signal?.aborted) throw new Error("Remote request cancelled");
|
|
63
|
+
throw error;
|
|
64
|
+
} finally {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
options.signal?.removeEventListener("abort", cancel);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
31
70
|
function validatePart(value: string, label: string): string {
|
|
32
71
|
const trimmed = value.trim();
|
|
33
72
|
if (!SLUG.test(trimmed) || trimmed === "." || trimmed === "..") {
|
|
@@ -58,38 +97,44 @@ export function remoteSkillsCacheRoot(): string {
|
|
|
58
97
|
export async function searchRemoteSkills(
|
|
59
98
|
query: string,
|
|
60
99
|
fetcher: FetchLike = fetch,
|
|
100
|
+
options: RemoteRequestOptions = {},
|
|
61
101
|
): Promise<RemoteSkillSearchResult[]> {
|
|
62
102
|
const trimmed = query.trim();
|
|
63
103
|
if (trimmed.length < 2 || trimmed.length > 200) {
|
|
64
104
|
throw new Error("Skills.sh search query must be between 2 and 200 characters");
|
|
65
105
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
106
|
+
return withRemoteDeadline(options, async (signal) => {
|
|
107
|
+
const url = `${SKILLS_SH}/api/search?${new URLSearchParams({ q: trimmed, limit: "10" })}`;
|
|
108
|
+
const response = await fetcher(url, {
|
|
109
|
+
headers: { Accept: "application/json" },
|
|
110
|
+
signal,
|
|
111
|
+
});
|
|
112
|
+
if (!response.ok) throw new Error(`Skills.sh search failed (${response.status})`);
|
|
113
|
+
const data = (await response.json()) as {
|
|
114
|
+
skills?: Array<{ id?: unknown; name?: unknown; installs?: unknown; source?: unknown }>;
|
|
115
|
+
};
|
|
116
|
+
return (data.skills ?? []).flatMap((skill) => {
|
|
117
|
+
if (
|
|
118
|
+
typeof skill.id !== "string" ||
|
|
119
|
+
typeof skill.name !== "string" ||
|
|
120
|
+
typeof skill.source !== "string"
|
|
121
|
+
)
|
|
122
|
+
return [];
|
|
123
|
+
try {
|
|
124
|
+
parseGitHubSource(skill.source);
|
|
125
|
+
validatePart(skill.name, "skill name");
|
|
126
|
+
return [
|
|
127
|
+
{
|
|
128
|
+
name: skill.name,
|
|
129
|
+
slug: skill.id,
|
|
130
|
+
source: skill.source,
|
|
131
|
+
installs: typeof skill.installs === "number" ? skill.installs : 0,
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
} catch {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
93
138
|
});
|
|
94
139
|
}
|
|
95
140
|
|
|
@@ -102,9 +147,13 @@ function frontmatterName(content: string): string | null {
|
|
|
102
147
|
async function fetchBytes(
|
|
103
148
|
fetcher: FetchLike,
|
|
104
149
|
url: string,
|
|
150
|
+
signal: AbortSignal,
|
|
105
151
|
maxBytes = MAX_FILE_BYTES,
|
|
106
152
|
): Promise<Buffer> {
|
|
107
|
-
const response = await fetcher(url, {
|
|
153
|
+
const response = await fetcher(url, {
|
|
154
|
+
headers: { Accept: "application/vnd.github+json" },
|
|
155
|
+
signal,
|
|
156
|
+
});
|
|
108
157
|
if (!response.ok) throw new Error(`Remote request failed (${response.status}): ${url}`);
|
|
109
158
|
const declared = Number(response.headers.get("content-length") ?? "0");
|
|
110
159
|
if (declared > maxBytes) throw new Error(`Remote file exceeds ${maxBytes} byte limit`);
|
|
@@ -116,9 +165,10 @@ async function fetchBytes(
|
|
|
116
165
|
async function fetchText(
|
|
117
166
|
fetcher: FetchLike,
|
|
118
167
|
url: string,
|
|
168
|
+
signal: AbortSignal,
|
|
119
169
|
maxBytes = MAX_FILE_BYTES,
|
|
120
170
|
): Promise<string> {
|
|
121
|
-
return (await fetchBytes(fetcher, url, maxBytes)).toString("utf-8");
|
|
171
|
+
return (await fetchBytes(fetcher, url, signal, maxBytes)).toString("utf-8");
|
|
122
172
|
}
|
|
123
173
|
|
|
124
174
|
function safeDestination(root: string, relativePath: string): string {
|
|
@@ -128,10 +178,11 @@ function safeDestination(root: string, relativePath: string): string {
|
|
|
128
178
|
return destination;
|
|
129
179
|
}
|
|
130
180
|
|
|
131
|
-
|
|
181
|
+
async function fetchRemoteSkillWithSignal(
|
|
132
182
|
source: string,
|
|
133
183
|
name: string,
|
|
134
|
-
options: { refresh?: boolean; fetcher?: FetchLike; cacheRoot?: string }
|
|
184
|
+
options: { refresh?: boolean; fetcher?: FetchLike; cacheRoot?: string },
|
|
185
|
+
signal: AbortSignal,
|
|
135
186
|
): Promise<RemoteSkillEntry> {
|
|
136
187
|
const { owner, repo } = parseGitHubSource(source);
|
|
137
188
|
const skillName = validatePart(name, "skill name");
|
|
@@ -152,6 +203,7 @@ export async function fetchRemoteSkill(
|
|
|
152
203
|
const treeText = await fetchText(
|
|
153
204
|
fetcher,
|
|
154
205
|
`${GITHUB_API}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/git/trees/HEAD?recursive=1`,
|
|
206
|
+
signal,
|
|
155
207
|
MAX_BUNDLE_BYTES,
|
|
156
208
|
);
|
|
157
209
|
let tree: {
|
|
@@ -181,7 +233,7 @@ export async function fetchRemoteSkill(
|
|
|
181
233
|
.split("/")
|
|
182
234
|
.map(encodeURIComponent)
|
|
183
235
|
.join("/")}`;
|
|
184
|
-
const content = await fetchText(fetcher, raw);
|
|
236
|
+
const content = await fetchText(fetcher, raw, signal);
|
|
185
237
|
if (frontmatterName(content) === skillName) {
|
|
186
238
|
skillPath = candidate.path;
|
|
187
239
|
skillContent = content;
|
|
@@ -215,6 +267,7 @@ export async function fetchRemoteSkill(
|
|
|
215
267
|
let writtenBytes = 0;
|
|
216
268
|
try {
|
|
217
269
|
for (const file of allowed) {
|
|
270
|
+
signal.throwIfAborted();
|
|
218
271
|
const rel = file.path.slice(prefix.length);
|
|
219
272
|
const destination = safeDestination(temporary, rel);
|
|
220
273
|
await mkdir(dirname(destination), { recursive: true });
|
|
@@ -225,7 +278,7 @@ export async function fetchRemoteSkill(
|
|
|
225
278
|
.split("/")
|
|
226
279
|
.map(encodeURIComponent)
|
|
227
280
|
.join("/")}`;
|
|
228
|
-
content = await fetchBytes(fetcher, raw);
|
|
281
|
+
content = await fetchBytes(fetcher, raw, signal);
|
|
229
282
|
}
|
|
230
283
|
writtenBytes += content.byteLength;
|
|
231
284
|
if (writtenBytes > MAX_BUNDLE_BYTES) {
|
|
@@ -251,3 +304,19 @@ export async function fetchRemoteSkill(
|
|
|
251
304
|
cached: false,
|
|
252
305
|
};
|
|
253
306
|
}
|
|
307
|
+
|
|
308
|
+
export async function fetchRemoteSkill(
|
|
309
|
+
source: string,
|
|
310
|
+
name: string,
|
|
311
|
+
options: {
|
|
312
|
+
refresh?: boolean;
|
|
313
|
+
fetcher?: FetchLike;
|
|
314
|
+
cacheRoot?: string;
|
|
315
|
+
signal?: AbortSignal;
|
|
316
|
+
timeoutMs?: number;
|
|
317
|
+
} = {},
|
|
318
|
+
): Promise<RemoteSkillEntry> {
|
|
319
|
+
return withRemoteDeadline(options, (signal) =>
|
|
320
|
+
fetchRemoteSkillWithSignal(source, name, options, signal),
|
|
321
|
+
);
|
|
322
|
+
}
|