insightfulpipe 0.2.0 → 0.3.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/CHANGELOG.md +45 -0
- package/README.md +40 -1
- package/package.json +2 -2
- package/src/api.js +86 -72
- package/src/cli/commands/discovery.js +2 -2
- package/src/cli/commands/execution.js +3 -3
- package/src/cli/commands/skills.js +454 -0
- package/src/cli/errors.js +159 -15
- package/src/cli/input.js +6 -0
- package/src/cli/program.js +7 -7
- package/src/cli/query-contexts.js +3 -2
- package/src/http.js +39 -0
- package/src/skills/agents.js +94 -0
- package/src/skills/catalog.js +177 -0
- package/src/skills/installer.js +331 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { printJson, printTable, wantsJson } from '../../output.js';
|
|
6
|
+
import { fail } from '../errors.js';
|
|
7
|
+
import { confirm } from '../input.js';
|
|
8
|
+
import { parseIntegerOption } from '../options.js';
|
|
9
|
+
import { AGENT_NAMES, AGENTS, DEFAULT_AGENT, resolveSkillsDir } from '../../skills/agents.js';
|
|
10
|
+
import {
|
|
11
|
+
fetchCatalog,
|
|
12
|
+
fetchSkillFiles,
|
|
13
|
+
fetchSkillZip,
|
|
14
|
+
getCatalogUrl,
|
|
15
|
+
pageUrl,
|
|
16
|
+
resolveSkill,
|
|
17
|
+
skillKey,
|
|
18
|
+
} from '../../skills/catalog.js';
|
|
19
|
+
import {
|
|
20
|
+
installSkill,
|
|
21
|
+
lexists,
|
|
22
|
+
listInstalled,
|
|
23
|
+
localChanges,
|
|
24
|
+
removeInstalled,
|
|
25
|
+
validateBundle,
|
|
26
|
+
} from '../../skills/installer.js';
|
|
27
|
+
|
|
28
|
+
const MCP_TIP =
|
|
29
|
+
'Skills read live data through the InsightfulPipe MCP. Connect it once: https://insightfulpipe.com/mcp-servers';
|
|
30
|
+
|
|
31
|
+
function withTargetOptions(command) {
|
|
32
|
+
return command
|
|
33
|
+
.option('--agent <agent>', `Agent to install for: ${AGENT_NAMES.join(', ')}`, DEFAULT_AGENT)
|
|
34
|
+
.option('-p, --project', "Use the current folder's project skills directory instead of the global one")
|
|
35
|
+
.option('--dir <path>', 'Use this skills directory instead');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function target(opts) {
|
|
39
|
+
try {
|
|
40
|
+
return resolveSkillsDir({ agent: opts.agent, project: Boolean(opts.project), dir: opts.dir });
|
|
41
|
+
} catch (error) {
|
|
42
|
+
fail(error.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function loadCatalog() {
|
|
47
|
+
try {
|
|
48
|
+
return await fetchCatalog();
|
|
49
|
+
} catch (error) {
|
|
50
|
+
fail(error.message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function installHint(skill, skills) {
|
|
55
|
+
const sameSlug = skills.filter((other) => other.slug === skill.slug).length > 1;
|
|
56
|
+
return `insightfulpipe skills install ${sameSlug ? skillKey(skill) : skill.slug}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function summarize(skill, skills) {
|
|
60
|
+
return {
|
|
61
|
+
skill: skill.slug,
|
|
62
|
+
category: skill.category,
|
|
63
|
+
name: skill.displayName,
|
|
64
|
+
description: skill.description,
|
|
65
|
+
version: skill.version ?? null,
|
|
66
|
+
install: installHint(skill, skills),
|
|
67
|
+
page: pageUrl(skill),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function short(text, max) {
|
|
72
|
+
const flat = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
73
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function agentLabel(where) {
|
|
77
|
+
return where.agent ? AGENTS[where.agent].label : 'the chosen folder';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Download, verify and write one skill. Returns a result row; never exits. */
|
|
81
|
+
/** Downloads one skill and checks it; `row` describes the failure when there is no `validated`. */
|
|
82
|
+
async function fetchChecked(skill) {
|
|
83
|
+
let bundle;
|
|
84
|
+
try {
|
|
85
|
+
bundle = await fetchSkillFiles(skill.category, skill.slug);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return { row: { skill: skillKey(skill), error: error.message } };
|
|
88
|
+
}
|
|
89
|
+
if (!bundle) {
|
|
90
|
+
return { row: { skill: skillKey(skill), error: 'The catalogue has no files for this skill.' } };
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
return { validated: validateBundle(bundle, skill) };
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return { row: { skill: skillKey(skill), error: error.message } };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function installChecked(skill, where, validated, force) {
|
|
100
|
+
try {
|
|
101
|
+
const { path, warning } = installSkill(where.dir, skill, validated, { force, source: getCatalogUrl() });
|
|
102
|
+
return { ok: true, row: { skill: skillKey(skill), version: validated.version, path, ...(warning ? { warning } : {}) } };
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return { ok: false, row: { skill: skillKey(skill), error: error.message } };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function installOne(skill, where, force) {
|
|
109
|
+
const fetched = await fetchChecked(skill);
|
|
110
|
+
return fetched.validated ? installChecked(skill, where, fetched.validated, force) : { ok: false, row: fetched.row };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function registerSkillsCommand(program) {
|
|
114
|
+
const skills = program
|
|
115
|
+
.command('skills')
|
|
116
|
+
.description('Find and install InsightfulPipe skills for Claude Code, OpenClaw, Codex, Cursor and Gemini CLI');
|
|
117
|
+
|
|
118
|
+
skills
|
|
119
|
+
.command('search')
|
|
120
|
+
.argument('[query...]', 'Words to look for in the skill name, category or description')
|
|
121
|
+
.option('-c, --category <category>', 'Only skills in this category (e.g., google-ads, seo)')
|
|
122
|
+
.option('--limit <n>', 'Show at most this many skills')
|
|
123
|
+
.description('Browse or search the skills catalogue')
|
|
124
|
+
.action(async (words, opts, command) => {
|
|
125
|
+
const catalog = await loadCatalog();
|
|
126
|
+
const terms = (words || []).join(' ').toLowerCase().split(/\s+/).filter(Boolean);
|
|
127
|
+
let found = catalog.skills.filter((skill) => {
|
|
128
|
+
if (opts.category && skill.category !== opts.category) return false;
|
|
129
|
+
const haystack = `${skill.slug} ${skill.displayName} ${skill.description} ${skill.category}`.toLowerCase();
|
|
130
|
+
return terms.every((term) => haystack.includes(term));
|
|
131
|
+
});
|
|
132
|
+
if (opts.limit) found = found.slice(0, parseIntegerOption('limit', opts.limit));
|
|
133
|
+
|
|
134
|
+
if (wantsJson(command)) {
|
|
135
|
+
printJson({ status: 'success', count: found.length, skills: found.map((skill) => summarize(skill, catalog.skills)) });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
printTable(
|
|
139
|
+
found.map((skill) => ({ skill: skill.slug, category: skill.category, description: short(skill.description, 70) })),
|
|
140
|
+
[
|
|
141
|
+
{ key: 'skill', label: 'Skill' },
|
|
142
|
+
{ key: 'category', label: 'Category' },
|
|
143
|
+
{ key: 'description', label: 'Description' },
|
|
144
|
+
],
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
skills
|
|
149
|
+
.command('info')
|
|
150
|
+
.argument('<skill>', 'Skill name, category/name, or @insightfulpipe/name')
|
|
151
|
+
.description('Show what a skill does, its files and how to install it')
|
|
152
|
+
.action(async (name, opts, command) => {
|
|
153
|
+
const catalog = await loadCatalog();
|
|
154
|
+
const found = resolveSkill(name, catalog.skills);
|
|
155
|
+
if (found.error) fail(found.error);
|
|
156
|
+
const skill = found.skill;
|
|
157
|
+
const info = {
|
|
158
|
+
...summarize(skill, catalog.skills),
|
|
159
|
+
files: (skill.files || []).map((file) => ({ path: file.path, size: file.size })),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
if (wantsJson(command)) {
|
|
163
|
+
printJson({ status: 'success', skill: info });
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
console.log(`${info.name} (${skillKey(skill)})`);
|
|
167
|
+
console.log(`Version: ${info.version ?? 'unknown'}`);
|
|
168
|
+
console.log(`Page: ${info.page}`);
|
|
169
|
+
console.log(`Install: ${info.install}`);
|
|
170
|
+
console.log(`Files: ${info.files.map((file) => file.path).join(', ') || 'unknown'}`);
|
|
171
|
+
console.log('');
|
|
172
|
+
console.log(info.description);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
withTargetOptions(
|
|
176
|
+
skills
|
|
177
|
+
.command('install')
|
|
178
|
+
.argument('[skills...]', 'One or more skills: name, category/name, or @insightfulpipe/name')
|
|
179
|
+
.option('-c, --category <category>', 'Install every skill in this category')
|
|
180
|
+
.option('-f, --force', 'Replace a skill folder that already exists')
|
|
181
|
+
.description('Download skills into your agent\'s skills folder (Claude Code by default)'),
|
|
182
|
+
).action(async (names, opts, command) => {
|
|
183
|
+
if (!names?.length && !opts.category) {
|
|
184
|
+
fail('Name at least one skill, or use --category.', 'Run: insightfulpipe skills search');
|
|
185
|
+
}
|
|
186
|
+
const where = target(opts);
|
|
187
|
+
const catalog = await loadCatalog();
|
|
188
|
+
|
|
189
|
+
const chosen = [];
|
|
190
|
+
for (const name of names || []) {
|
|
191
|
+
const found = resolveSkill(name, catalog.skills);
|
|
192
|
+
if (found.error) fail(found.error);
|
|
193
|
+
chosen.push(found.skill);
|
|
194
|
+
}
|
|
195
|
+
if (opts.category) {
|
|
196
|
+
const inCategory = catalog.skills.filter((skill) => skill.category === opts.category);
|
|
197
|
+
if (!inCategory.length) fail(`No skills in category "${opts.category}".`, 'Run: insightfulpipe skills search');
|
|
198
|
+
chosen.push(...inCategory);
|
|
199
|
+
}
|
|
200
|
+
const unique = [...new Map(chosen.map((skill) => [skillKey(skill), skill])).values()];
|
|
201
|
+
|
|
202
|
+
const installed = [];
|
|
203
|
+
const failed = [];
|
|
204
|
+
for (const skill of unique) {
|
|
205
|
+
const result = await installOne(skill, where, Boolean(opts.force));
|
|
206
|
+
(result.ok ? installed : failed).push(result.row);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (wantsJson(command)) {
|
|
210
|
+
const report = { agent: where.agent, scope: where.scope, skills_dir: where.dir, installed, failed };
|
|
211
|
+
if (!installed.length) fail('No skill was installed.', undefined, report);
|
|
212
|
+
printJson({
|
|
213
|
+
status: failed.length ? 'partial' : 'success',
|
|
214
|
+
...report,
|
|
215
|
+
next_step: `Start a new ${agentLabel(where)} session to load the skill. ${MCP_TIP}`,
|
|
216
|
+
});
|
|
217
|
+
} else {
|
|
218
|
+
for (const row of installed) console.log(`Installed ${row.skill} (${row.version}) -> ${row.path}`);
|
|
219
|
+
for (const row of installed) if (row.warning) console.error(`Warning: ${row.warning}`);
|
|
220
|
+
for (const row of failed) console.error(`Failed ${row.skill}: ${row.error}`);
|
|
221
|
+
if (installed.length) console.log(`\nStart a new ${agentLabel(where)} session to load it. ${MCP_TIP}`);
|
|
222
|
+
}
|
|
223
|
+
if (failed.length) process.exitCode = 1;
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
withTargetOptions(
|
|
227
|
+
skills.command('list').description('List the InsightfulPipe skills installed in a skills folder'),
|
|
228
|
+
).action(async (opts, command) => {
|
|
229
|
+
const where = target(opts);
|
|
230
|
+
const installed = listInstalled(where.dir);
|
|
231
|
+
|
|
232
|
+
let catalog = null;
|
|
233
|
+
if (installed.length) {
|
|
234
|
+
try {
|
|
235
|
+
catalog = await fetchCatalog();
|
|
236
|
+
} catch {
|
|
237
|
+
catalog = null; // Offline still lists what is installed; it just can't say what is newer.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const latestFor = (marker) =>
|
|
241
|
+
catalog?.skills.find((skill) => skill.category === marker.category && skill.slug === marker.slug)?.version ?? null;
|
|
242
|
+
|
|
243
|
+
const rows = installed.map(({ dir, marker }) => {
|
|
244
|
+
const latest = latestFor(marker);
|
|
245
|
+
let edited = null;
|
|
246
|
+
let problem;
|
|
247
|
+
try {
|
|
248
|
+
edited = localChanges(dir, marker).edited;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
problem = `Could not check for local edits: ${error.message}`;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
skill: `${marker.category}/${marker.slug}`,
|
|
254
|
+
version: marker.version,
|
|
255
|
+
latest,
|
|
256
|
+
update_available: Boolean(latest && latest !== marker.version),
|
|
257
|
+
locally_edited: edited,
|
|
258
|
+
path: dir,
|
|
259
|
+
...(problem ? { error: problem } : {}),
|
|
260
|
+
};
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
if (wantsJson(command)) {
|
|
264
|
+
printJson({
|
|
265
|
+
status: 'success',
|
|
266
|
+
agent: where.agent,
|
|
267
|
+
scope: where.scope,
|
|
268
|
+
skills_dir: where.dir,
|
|
269
|
+
catalog: installed.length ? (catalog ? 'checked' : 'unreachable') : 'not needed',
|
|
270
|
+
skills: rows,
|
|
271
|
+
});
|
|
272
|
+
if (rows.some((row) => row.error)) process.exitCode = 1;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
printTable(
|
|
276
|
+
rows.map((row) => ({
|
|
277
|
+
skill: row.skill,
|
|
278
|
+
version: row.version,
|
|
279
|
+
status: row.update_available ? `update: ${row.latest}` : row.latest ? 'up to date' : 'unknown',
|
|
280
|
+
edited: row.error ? 'unknown' : row.locally_edited ? 'yes' : '',
|
|
281
|
+
})),
|
|
282
|
+
[
|
|
283
|
+
{ key: 'skill', label: 'Skill' },
|
|
284
|
+
{ key: 'version', label: 'Version' },
|
|
285
|
+
{ key: 'status', label: 'Status' },
|
|
286
|
+
{ key: 'edited', label: 'Edited' },
|
|
287
|
+
],
|
|
288
|
+
);
|
|
289
|
+
for (const row of rows) {
|
|
290
|
+
if (row.error) {
|
|
291
|
+
console.error(`${row.skill}: ${row.error}`);
|
|
292
|
+
process.exitCode = 1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
withTargetOptions(
|
|
298
|
+
skills
|
|
299
|
+
.command('update')
|
|
300
|
+
.argument('[skills...]', 'Installed skills to update')
|
|
301
|
+
.option('--all', 'Update every InsightfulPipe skill in the folder')
|
|
302
|
+
.option('-f, --force', 'Update even if you edited the installed files (your edits are replaced)')
|
|
303
|
+
.description('Update installed skills to the latest version, keeping your local edits unless --force'),
|
|
304
|
+
).action(async (names, opts, command) => {
|
|
305
|
+
if (!names?.length && !opts.all) {
|
|
306
|
+
fail('Name the skills to update, or use --all.');
|
|
307
|
+
}
|
|
308
|
+
const where = target(opts);
|
|
309
|
+
const installed = listInstalled(where.dir);
|
|
310
|
+
|
|
311
|
+
const targets = [];
|
|
312
|
+
if (opts.all) {
|
|
313
|
+
targets.push(...installed);
|
|
314
|
+
} else {
|
|
315
|
+
for (const name of names) {
|
|
316
|
+
const wanted = String(name).toLowerCase().replace(/^@insightfulpipe\//, '');
|
|
317
|
+
const matches = installed.filter(
|
|
318
|
+
({ marker }) => marker.slug === wanted || `${marker.category}/${marker.slug}` === wanted,
|
|
319
|
+
);
|
|
320
|
+
if (matches.length !== 1) {
|
|
321
|
+
fail(matches.length ? `"${name}" matches more than one installed skill.` : `"${name}" is not installed in ${where.dir}.`);
|
|
322
|
+
}
|
|
323
|
+
targets.push(matches[0]);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const catalog = targets.length ? await loadCatalog() : { skills: [] };
|
|
328
|
+
const result = { updated: [], up_to_date: [], skipped: [], failed: [] };
|
|
329
|
+
for (const { dir, marker } of targets) {
|
|
330
|
+
const key = `${marker.category}/${marker.slug}`;
|
|
331
|
+
const latest = catalog.skills.find((skill) => skill.category === marker.category && skill.slug === marker.slug);
|
|
332
|
+
if (!latest) {
|
|
333
|
+
result.skipped.push({ skill: key, reason: 'It is no longer in the catalogue; left as it is.' });
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (latest.version && latest.version === marker.version) {
|
|
337
|
+
result.up_to_date.push({ skill: key, version: marker.version });
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const fetched = await fetchChecked(latest);
|
|
341
|
+
if (!fetched.validated) {
|
|
342
|
+
result.failed.push(fetched.row);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
let changes;
|
|
346
|
+
try {
|
|
347
|
+
changes = localChanges(dir, marker);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
result.failed.push({ skill: key, error: `Could not check for local edits: ${error.message}` });
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (changes.edited && !opts.force) {
|
|
353
|
+
result.skipped.push({
|
|
354
|
+
skill: key,
|
|
355
|
+
reason: 'You edited the installed files. Re-run with --force to replace them.',
|
|
356
|
+
changed: changes.changed,
|
|
357
|
+
missing: changes.missing,
|
|
358
|
+
added: changes.added,
|
|
359
|
+
});
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
const outcome = installChecked(latest, where, fetched.validated, true);
|
|
363
|
+
if (outcome.ok) result.updated.push({ ...outcome.row, from: marker.version });
|
|
364
|
+
else result.failed.push(outcome.row);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (wantsJson(command)) {
|
|
368
|
+
if (result.failed.length && !result.updated.length) fail('No skill was updated.', undefined, { skills_dir: where.dir, ...result });
|
|
369
|
+
printJson({ status: result.failed.length ? 'partial' : 'success', skills_dir: where.dir, ...result });
|
|
370
|
+
} else {
|
|
371
|
+
for (const row of result.updated) console.log(`Updated ${row.skill} ${row.from} -> ${row.version}`);
|
|
372
|
+
for (const row of result.updated) if (row.warning) console.error(`Warning: ${row.warning}`);
|
|
373
|
+
for (const row of result.up_to_date) console.log(`Up to date ${row.skill} (${row.version})`);
|
|
374
|
+
for (const row of result.skipped) console.log(`Skipped ${row.skill}: ${row.reason}`);
|
|
375
|
+
for (const row of result.failed) console.error(`Failed ${row.skill}: ${row.error}`);
|
|
376
|
+
if (!targets.length) console.log('No InsightfulPipe skills installed here.');
|
|
377
|
+
}
|
|
378
|
+
if (result.failed.length) process.exitCode = 1;
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
withTargetOptions(
|
|
382
|
+
skills
|
|
383
|
+
.command('remove')
|
|
384
|
+
.argument('<skills...>', 'Installed skills to remove')
|
|
385
|
+
.option('-y, --yes', 'Skip the confirmation prompt')
|
|
386
|
+
.description('Remove installed InsightfulPipe skills (only folders this CLI installed)'),
|
|
387
|
+
).action(async (names, opts, command) => {
|
|
388
|
+
const where = target(opts);
|
|
389
|
+
const installed = listInstalled(where.dir);
|
|
390
|
+
const targets = names.map((name) => {
|
|
391
|
+
const wanted = String(name).toLowerCase().replace(/^@insightfulpipe\//, '');
|
|
392
|
+
const matches = installed.filter(
|
|
393
|
+
({ marker }) => marker.slug === wanted || `${marker.category}/${marker.slug}` === wanted,
|
|
394
|
+
);
|
|
395
|
+
if (matches.length !== 1) {
|
|
396
|
+
fail(matches.length ? `"${name}" matches more than one installed skill.` : `"${name}" is not an InsightfulPipe skill installed in ${where.dir}.`);
|
|
397
|
+
}
|
|
398
|
+
return matches[0];
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
if (!opts.yes) {
|
|
402
|
+
if (!process.stdin.isTTY) fail('Removing skills needs confirmation in non-interactive mode. Re-run with --yes.');
|
|
403
|
+
const ok = await confirm(`Remove ${targets.map((t) => t.slug).join(', ')} from ${where.dir}? [y/N] `);
|
|
404
|
+
if (!ok) fail('Nothing removed.');
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const removed = [];
|
|
408
|
+
for (const { dir, marker } of targets) {
|
|
409
|
+
try {
|
|
410
|
+
removeInstalled(dir);
|
|
411
|
+
removed.push({ skill: `${marker.category}/${marker.slug}`, path: dir });
|
|
412
|
+
} catch (error) {
|
|
413
|
+
fail(error.message);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (wantsJson(command)) printJson({ status: 'success', removed });
|
|
417
|
+
else for (const row of removed) console.log(`Removed ${row.skill} (${row.path})`);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
skills
|
|
421
|
+
.command('download')
|
|
422
|
+
.argument('<skill>', 'Skill name, category/name, or @insightfulpipe/name')
|
|
423
|
+
.option('-o, --output <file>', 'Where to save the ZIP (default: <skill>.zip in this folder)')
|
|
424
|
+
.option('-f, --force', 'Overwrite the file if it exists')
|
|
425
|
+
.description('Save a skill as a ZIP, for uploading in Claude.ai or Claude Desktop')
|
|
426
|
+
.action(async (name, opts, command) => {
|
|
427
|
+
const catalog = await loadCatalog();
|
|
428
|
+
const found = resolveSkill(name, catalog.skills);
|
|
429
|
+
if (found.error) fail(found.error);
|
|
430
|
+
const skill = found.skill;
|
|
431
|
+
const output = resolve(opts.output || `${skill.slug}.zip`);
|
|
432
|
+
if (lexists(output) && !opts.force) fail(`${output} already exists. Add --force to overwrite it.`);
|
|
433
|
+
|
|
434
|
+
let zip;
|
|
435
|
+
try {
|
|
436
|
+
zip = await fetchSkillZip(skill.category, skill.slug);
|
|
437
|
+
} catch (error) {
|
|
438
|
+
fail(error.message);
|
|
439
|
+
}
|
|
440
|
+
if (!zip) fail('The catalogue has no ZIP for this skill.');
|
|
441
|
+
const partial = `${output}.${randomBytes(4).toString('hex')}.part`;
|
|
442
|
+
try {
|
|
443
|
+
writeFileSync(partial, zip, { flag: 'wx' });
|
|
444
|
+
renameSync(partial, output);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
rmSync(partial, { force: true });
|
|
447
|
+
fail(`Could not save ${output}: ${error.message}`);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const next = 'Upload this ZIP as a custom skill in Claude.ai or Claude Desktop.';
|
|
451
|
+
if (wantsJson(command)) printJson({ status: 'success', skill: skillKey(skill), path: output, bytes: zip.length, next_step: next });
|
|
452
|
+
else console.log(`Saved ${skillKey(skill)} to ${output}\n${next}`);
|
|
453
|
+
});
|
|
454
|
+
}
|
package/src/cli/errors.js
CHANGED
|
@@ -1,32 +1,176 @@
|
|
|
1
|
+
/** Anything meant for reading is cut here; the marker says how much the server actually sent. */
|
|
2
|
+
const MAX_TEXT_CHARS = 2000;
|
|
3
|
+
const TRUNCATION_MARKER = /\.\.\. \[truncated, \d+ more characters\]$/;
|
|
4
|
+
|
|
5
|
+
/** Idempotent: text that is already a full head plus the marker passes through, so a string cut
|
|
6
|
+
* once in api.js is not cut again on its way out. */
|
|
7
|
+
export function bounded(text) {
|
|
8
|
+
if (text.length <= MAX_TEXT_CHARS || text.replace(TRUNCATION_MARKER, '').length === MAX_TEXT_CHARS) {
|
|
9
|
+
return text;
|
|
10
|
+
}
|
|
11
|
+
return `${text.slice(0, MAX_TEXT_CHARS)}... [truncated, ${text.length - MAX_TEXT_CHARS} more characters]`;
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
/** Failures follow the same contract as results: JSON by default, a sentence under `--table`.
|
|
2
15
|
* Always on stderr, so `insightfulpipe ... | jq` never sees them either way. */
|
|
3
16
|
function humanOutput() {
|
|
4
17
|
return process.argv.includes('--table');
|
|
5
18
|
}
|
|
6
19
|
|
|
7
|
-
|
|
20
|
+
/** A piped stderr is written asynchronously on macOS, and `process.exit` drops whatever has not
|
|
21
|
+
* drained yet (a 64 KB cut); blocking mode makes the write finish before the exit. */
|
|
22
|
+
function writeStderr(text) {
|
|
23
|
+
process.stderr._handle?.setBlocking?.(true);
|
|
24
|
+
process.stderr.write(`${text}\n`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function fail(message, tip, details = {}) {
|
|
8
28
|
if (humanOutput()) {
|
|
9
|
-
|
|
10
|
-
if (tip) {
|
|
11
|
-
console.error(`Tip: ${tip}`);
|
|
12
|
-
}
|
|
29
|
+
writeStderr(tip ? `Error: ${message}\nTip: ${tip}` : `Error: ${message}`);
|
|
13
30
|
} else {
|
|
14
|
-
|
|
31
|
+
writeStderr(JSON.stringify({ status: 'error', message, ...(tip ? { tip } : {}), ...details }, null, 2));
|
|
15
32
|
}
|
|
16
33
|
process.exit(1);
|
|
17
34
|
}
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
/** What the server sent next to its message rides along in the JSON output, so a script can branch
|
|
37
|
+
* on the platform's own code, sleep for `retry_after`, or see that a write may have landed. */
|
|
38
|
+
const PASSTHROUGH_FIELDS = ['api_status_code', 'api_error', 'details', 'error_type', 'retry_after'];
|
|
39
|
+
// The app's own write-outcome fields (apps/raw_apis/error_text.py), so a retrying script never double-writes.
|
|
40
|
+
const WRITE_OUTCOME_FIELDS = [
|
|
41
|
+
'status_code',
|
|
42
|
+
'write_state',
|
|
43
|
+
'outcome',
|
|
44
|
+
'needs_reconciliation',
|
|
45
|
+
'may_have_written',
|
|
46
|
+
'ambiguous',
|
|
47
|
+
'ambiguous_write',
|
|
48
|
+
'outcome_unknown',
|
|
49
|
+
'created_resource_names',
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/** Slug words whose brand spelling title-casing gets wrong; every other word is simply capitalised. */
|
|
53
|
+
const BRAND_WORDS = {
|
|
54
|
+
api: 'API',
|
|
55
|
+
bigquery: 'BigQuery',
|
|
56
|
+
callrail: 'CallRail',
|
|
57
|
+
dataforseo: 'DataForSEO',
|
|
58
|
+
linkedin: 'LinkedIn',
|
|
59
|
+
mssql: 'MSSQL',
|
|
60
|
+
mysql: 'MySQL',
|
|
61
|
+
openai: 'OpenAI',
|
|
62
|
+
pagespeed: 'PageSpeed',
|
|
63
|
+
postgresql: 'PostgreSQL',
|
|
64
|
+
rest: 'REST',
|
|
65
|
+
tiktok: 'TikTok',
|
|
66
|
+
woocommerce: 'WooCommerce',
|
|
67
|
+
wordpress: 'WordPress',
|
|
68
|
+
youtube: 'YouTube',
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** The platform named in the response wins, then the one the command was run for: "tiktok-ads"
|
|
72
|
+
* reads as "TikTok Ads". With neither, or a slug with no words in it, the label is just "API". */
|
|
73
|
+
function platformLabel(data, context) {
|
|
74
|
+
const slug = [data.platform, context.platform].find((value) => typeof value === 'string' && value.trim());
|
|
75
|
+
const label = (slug || '')
|
|
76
|
+
.toLowerCase()
|
|
77
|
+
.split(/[-_\s]+/)
|
|
78
|
+
.filter(Boolean)
|
|
79
|
+
.map((word) => BRAND_WORDS[word] || word[0].toUpperCase() + word.slice(1))
|
|
80
|
+
.join(' ');
|
|
81
|
+
return label || 'API';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A string is itself and an object or array is compact JSON; anything else (the boolean error
|
|
85
|
+
* flag, a bare number) is not a message. */
|
|
86
|
+
function asText(value) {
|
|
87
|
+
if (typeof value === 'string') {
|
|
88
|
+
return value || undefined;
|
|
89
|
+
}
|
|
90
|
+
if (value && typeof value === 'object') {
|
|
91
|
+
return JSON.stringify(value);
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Providers send `api_error` as a string, as `{code, message}` (Pinterest) or as
|
|
97
|
+
* `{error: {code, message}}` (Meta); anything else is shown as JSON rather than dropped. */
|
|
98
|
+
function apiErrorParts(apiError) {
|
|
99
|
+
if (typeof apiError === 'string') {
|
|
100
|
+
return apiError ? { message: apiError } : undefined;
|
|
101
|
+
}
|
|
102
|
+
if (!apiError || typeof apiError !== 'object') {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
const inner = apiError.error && typeof apiError.error === 'object' ? apiError.error : apiError;
|
|
106
|
+
if (typeof inner.message !== 'string' || !inner.message) {
|
|
107
|
+
return { message: JSON.stringify(apiError) };
|
|
108
|
+
}
|
|
109
|
+
return { code: inner.code, message: inner.message };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A code is normally a number or a short string; a structured one is shown compact, never as
|
|
113
|
+
* "[object Object]". */
|
|
114
|
+
function codeText(code) {
|
|
115
|
+
if (code === undefined || code === null || code === '') {
|
|
116
|
+
return '';
|
|
117
|
+
}
|
|
118
|
+
return ` ${typeof code === 'object' ? JSON.stringify(code) : code}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `Pinterest Pages error 50 (HTTP 404): Pin not found.` keeps the platform's code, its message and
|
|
122
|
+
* the status it answered with, so the sentence under --table says as much as the JSON does. */
|
|
123
|
+
function describeApiError(data, context) {
|
|
124
|
+
const parts = apiErrorParts(data.api_error);
|
|
125
|
+
if (!parts) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const http = data.api_status_code === undefined || data.api_status_code === null ? '' : ` (HTTP ${data.api_status_code})`;
|
|
129
|
+
return `${platformLabel(data, context)} error${codeText(parts.code)}${http}: ${parts.message}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The one sentence for a failed response, in precedence order: the server's own message, then the
|
|
133
|
+
* platform's `api_error`, then `details`, then a string `error`; the boolean `error` flag never shows. */
|
|
134
|
+
export function errorMessage(data, context = {}) {
|
|
135
|
+
return bounded(
|
|
136
|
+
asText(data.message) ||
|
|
137
|
+
describeApiError(data, context) ||
|
|
138
|
+
asText(data.details) ||
|
|
139
|
+
(typeof data.error === 'string' ? data.error : undefined) ||
|
|
140
|
+
(typeof data.status === 'number' ? `Request failed with status ${data.status}` : 'Request failed'),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A field passes through as sent while it is small; past the cap it becomes a truncated string,
|
|
145
|
+
* so a 2 MB upstream body (an HTML error page in `api_error`, say) never lands in the output whole. */
|
|
146
|
+
function boundedField(value) {
|
|
147
|
+
if (typeof value === 'string') {
|
|
148
|
+
return bounded(value);
|
|
149
|
+
}
|
|
150
|
+
const serialized = JSON.stringify(value);
|
|
151
|
+
return serialized === undefined || serialized.length <= MAX_TEXT_CHARS ? value : bounded(serialized);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function retryTip(data) {
|
|
155
|
+
const seconds = Number(data.retry_after);
|
|
156
|
+
return Number.isFinite(seconds) && seconds > 0 ? `Retry after ${seconds} seconds.` : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** `context.platform` is the slug the command was run for (`query pinterest-pages ...`), so the
|
|
160
|
+
* sentence can name the platform even when the response does not. */
|
|
161
|
+
export function handleApiError(data, context = {}) {
|
|
20
162
|
if (!data || !data.error) {
|
|
21
163
|
return data;
|
|
22
164
|
}
|
|
23
165
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
data
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
166
|
+
const passthrough = {};
|
|
167
|
+
for (const field of [...PASSTHROUGH_FIELDS, ...WRITE_OUTCOME_FIELDS]) {
|
|
168
|
+
if (data[field] !== undefined) {
|
|
169
|
+
passthrough[field] = boundedField(data[field]);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (typeof data.status === 'number') {
|
|
173
|
+
passthrough.http_status = data.status;
|
|
174
|
+
}
|
|
175
|
+
fail(errorMessage(data, context), data.tip || retryTip(data), passthrough);
|
|
32
176
|
}
|
package/src/cli/input.js
CHANGED
|
@@ -122,6 +122,12 @@ export async function promptSecret(promptText) {
|
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
/** Ask a yes/no question on the terminal; anything but y/yes is a no. */
|
|
126
|
+
export async function confirm(question) {
|
|
127
|
+
const answer = await promptInput(question);
|
|
128
|
+
return /^(y|yes)$/i.test(answer);
|
|
129
|
+
}
|
|
130
|
+
|
|
125
131
|
export async function confirmWriteAction(platform, body, skipConfirmation) {
|
|
126
132
|
if (skipConfirmation) {
|
|
127
133
|
return;
|