@knolo/cli 6.2.8 → 6.2.10
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 +31 -4
- package/bin/compose.mjs +44 -40
- package/bin/core-load-hook.mjs +33 -0
- package/bin/flight.mjs +10 -14
- package/bin/knolo.mjs +54 -18
- package/bin/load-core.mjs +76 -0
- package/bin/persona.mjs +981 -0
- package/bin/registry/ask.mjs +334 -0
- package/bin/registry/categories.mjs +31 -0
- package/bin/registry/commands.mjs +14 -1
- package/bin/research.mjs +7 -12
- package/bin/skill.mjs +43 -32
- package/package.json +2 -2
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import {
|
|
3
|
+
normalizeRegistryUrl,
|
|
4
|
+
registryApiUrl,
|
|
5
|
+
resolveRegistryUrl,
|
|
6
|
+
} from './config.mjs';
|
|
7
|
+
import { normalizeCategories } from './categories.mjs';
|
|
8
|
+
import { loadCredentials } from './credentials.mjs';
|
|
9
|
+
import { RegistryError, RegistryNetworkError } from './errors.mjs';
|
|
10
|
+
import { postJson } from './http.mjs';
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_MODEL_BASE_URL = 'http://127.0.0.1:11434/v1';
|
|
13
|
+
export const ASK_SYSTEM_PROMPT =
|
|
14
|
+
'Answer only from the excerpts and any skill instructions in the prompt. Cite each knowledge claim with publisher/slug@version and the object id. Cite a skill as publisher/name. Do not use outside knowledge.';
|
|
15
|
+
|
|
16
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
17
|
+
|
|
18
|
+
export function excerptDigest(excerpt) {
|
|
19
|
+
return createHash('sha256').update(excerpt, 'utf8').digest('hex');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function citation(hit) {
|
|
23
|
+
return `${hit.publisher}/${hit.slug}@${hit.version} ${hit.objectId}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function normalizeModelBaseUrl(value) {
|
|
27
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
28
|
+
throw new Error('Model base URL must be an HTTP(S) URL.');
|
|
29
|
+
}
|
|
30
|
+
let url;
|
|
31
|
+
try {
|
|
32
|
+
url = new URL(value.trim());
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error(`Invalid model base URL: ${value}`);
|
|
35
|
+
}
|
|
36
|
+
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
37
|
+
throw new Error('Model base URL must use http or https.');
|
|
38
|
+
}
|
|
39
|
+
if (url.username || url.password) {
|
|
40
|
+
throw new Error('Model base URL must not include credentials.');
|
|
41
|
+
}
|
|
42
|
+
url.search = '';
|
|
43
|
+
url.hash = '';
|
|
44
|
+
const pathname = url.pathname.replace(/\/+$/, '');
|
|
45
|
+
return `${url.origin}${pathname === '/' ? '' : pathname}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function chatCompletionsUrl(baseUrl) {
|
|
49
|
+
return `${normalizeModelBaseUrl(baseUrl)}/chat/completions`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function parseAskArgs(args) {
|
|
53
|
+
const positional = [];
|
|
54
|
+
const flags = {};
|
|
55
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
56
|
+
const arg = args[i];
|
|
57
|
+
if (arg === '--evidence') {
|
|
58
|
+
flags.evidence = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (arg === '--all') {
|
|
62
|
+
flags.all = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (arg === '--category') {
|
|
66
|
+
const value = args[++i];
|
|
67
|
+
if (value === undefined || value.startsWith('--')) {
|
|
68
|
+
throw new Error('Missing value for --category');
|
|
69
|
+
}
|
|
70
|
+
flags.categories = [...(flags.categories ?? []), value];
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (['--model', '--base-url', '--limit', '--hub', '--registry'].includes(arg)) {
|
|
74
|
+
const value = args[++i];
|
|
75
|
+
if (value === undefined || value.startsWith('--')) {
|
|
76
|
+
throw new Error(`Missing value for ${arg}`);
|
|
77
|
+
}
|
|
78
|
+
if (arg === '--hub' || arg === '--registry') flags.hub = value;
|
|
79
|
+
else flags[arg.slice(2)] = value;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (arg.startsWith('--')) throw new Error(`Unknown flag for ask: ${arg}`);
|
|
83
|
+
positional.push(arg);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const question = positional.join(' ').trim();
|
|
87
|
+
if (!question) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
'Usage: knolo ask "<question>" [--category <id>] [--all] [--model <name>] [--base-url <url>] [--limit <n>] [--evidence] [--hub <url>]'
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (flags.all && flags.categories) {
|
|
93
|
+
throw new Error('Use either --all or --category, not both.');
|
|
94
|
+
}
|
|
95
|
+
if (!flags.all && !flags.categories) {
|
|
96
|
+
throw new Error('Pick one category with --category, or pass --all to search the entire hub.');
|
|
97
|
+
}
|
|
98
|
+
if (flags.categories) {
|
|
99
|
+
flags.categories = normalizeCategories(flags.categories, { min: 1, max: 8 });
|
|
100
|
+
}
|
|
101
|
+
if (flags.limit !== undefined) {
|
|
102
|
+
if (!/^[1-9]\d*$/.test(flags.limit)) {
|
|
103
|
+
throw new Error('--limit must be an integer from 1 to 20.');
|
|
104
|
+
}
|
|
105
|
+
const limit = Number(flags.limit);
|
|
106
|
+
if (limit < 1 || limit > 20) {
|
|
107
|
+
throw new Error('--limit must be an integer from 1 to 20.');
|
|
108
|
+
}
|
|
109
|
+
flags.limit = limit;
|
|
110
|
+
}
|
|
111
|
+
return { question, flags };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function modelName(flags, env) {
|
|
115
|
+
const name = flags.model || env.KNOLO_MODEL;
|
|
116
|
+
return typeof name === 'string' && name.trim() ? name.trim() : '';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function modelBase(flags, env) {
|
|
120
|
+
return normalizeModelBaseUrl(
|
|
121
|
+
flags['base-url'] || env.KNOLO_MODEL_BASE_URL || DEFAULT_MODEL_BASE_URL
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function hubToken(registry, { env, homeDir }) {
|
|
126
|
+
try {
|
|
127
|
+
const credentials = await loadCredentials({ env, homeDir });
|
|
128
|
+
if (normalizeRegistryUrl(credentials.registry) !== normalizeRegistryUrl(registry)) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return credentials.token;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (error instanceof Error && error.message.startsWith('Not logged in')) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function asHit(value) {
|
|
141
|
+
if (!value || typeof value !== 'object') return null;
|
|
142
|
+
const hit = value;
|
|
143
|
+
if (typeof hit.publisher !== 'string' || !hit.publisher.trim()) return null;
|
|
144
|
+
if (typeof hit.slug !== 'string' || !hit.slug.trim()) return null;
|
|
145
|
+
if (typeof hit.version !== 'string' || !hit.version.trim()) return null;
|
|
146
|
+
if (typeof hit.objectId !== 'string' || !hit.objectId.trim()) return null;
|
|
147
|
+
if (typeof hit.excerpt !== 'string' || !hit.excerpt) return null;
|
|
148
|
+
if (typeof hit.excerptSha256 !== 'string' || !SHA256_HEX.test(hit.excerptSha256.toLowerCase())) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
if (excerptDigest(hit.excerpt) !== hit.excerptSha256.toLowerCase()) return null;
|
|
152
|
+
return {
|
|
153
|
+
publisher: hit.publisher,
|
|
154
|
+
slug: hit.slug,
|
|
155
|
+
version: hit.version,
|
|
156
|
+
objectId: hit.objectId,
|
|
157
|
+
heading: typeof hit.heading === 'string' ? hit.heading : '',
|
|
158
|
+
excerpt: hit.excerpt,
|
|
159
|
+
excerptSha256: hit.excerptSha256.toLowerCase(),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function verifiedHits(hits) {
|
|
164
|
+
if (!Array.isArray(hits)) return [];
|
|
165
|
+
return hits.map(asHit).filter((hit) => hit !== null);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function asSkill(value) {
|
|
169
|
+
if (!value || typeof value !== 'object') return null;
|
|
170
|
+
const skill = value;
|
|
171
|
+
if (typeof skill.publisher !== 'string' || !skill.publisher.trim()) return null;
|
|
172
|
+
if (typeof skill.name !== 'string' || !skill.name.trim()) return null;
|
|
173
|
+
if (typeof skill.excerpt !== 'string' || !skill.excerpt) return null;
|
|
174
|
+
if (typeof skill.excerptSha256 !== 'string' || !SHA256_HEX.test(skill.excerptSha256.toLowerCase())) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
if (excerptDigest(skill.excerpt) !== skill.excerptSha256.toLowerCase()) return null;
|
|
178
|
+
if (typeof skill.skillSha256 !== 'string' || !SHA256_HEX.test(skill.skillSha256.toLowerCase())) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
publisher: skill.publisher,
|
|
183
|
+
name: skill.name,
|
|
184
|
+
description: typeof skill.description === 'string' ? skill.description : '',
|
|
185
|
+
excerpt: skill.excerpt,
|
|
186
|
+
excerptSha256: skill.excerptSha256.toLowerCase(),
|
|
187
|
+
skillSha256: skill.skillSha256.toLowerCase(),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function verifiedSkills(skills) {
|
|
192
|
+
if (!Array.isArray(skills)) return [];
|
|
193
|
+
return skills.map(asSkill).filter((skill) => skill !== null);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function printEvidence(skills, hits, print) {
|
|
197
|
+
for (const skill of skills) {
|
|
198
|
+
print(`${skill.publisher}/${skill.name}`);
|
|
199
|
+
if (skill.description) print(skill.description);
|
|
200
|
+
print(skill.excerpt);
|
|
201
|
+
print(skill.excerptSha256);
|
|
202
|
+
print('');
|
|
203
|
+
}
|
|
204
|
+
for (const hit of hits) {
|
|
205
|
+
print(citation(hit));
|
|
206
|
+
if (hit.heading) print(hit.heading);
|
|
207
|
+
print(hit.excerpt);
|
|
208
|
+
print(hit.excerptSha256);
|
|
209
|
+
print('');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function answerText(body) {
|
|
214
|
+
const content = body?.choices?.[0]?.message?.content;
|
|
215
|
+
return typeof content === 'string' ? content.trim() : '';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Queries Hub, checks excerpt hashes, then asks the user's chat endpoint.
|
|
220
|
+
* The model key is sent only to that endpoint. Hub never receives it.
|
|
221
|
+
*/
|
|
222
|
+
export async function runHubAsk(
|
|
223
|
+
args,
|
|
224
|
+
{
|
|
225
|
+
env = process.env,
|
|
226
|
+
fetchImpl = globalThis.fetch,
|
|
227
|
+
homeDir,
|
|
228
|
+
print = console.log,
|
|
229
|
+
printError = console.error,
|
|
230
|
+
} = {}
|
|
231
|
+
) {
|
|
232
|
+
const { question, flags } = parseAskArgs(args);
|
|
233
|
+
const model = modelName(flags, env);
|
|
234
|
+
if (!flags.evidence && !model) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
'Set --model or KNOLO_MODEL. Use --evidence to print Hub excerpts without a model.'
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const registry = resolveRegistryUrl({ value: flags.hub, env });
|
|
241
|
+
const url = registryApiUrl(registry, ['knowledge', 'query']);
|
|
242
|
+
const token = await hubToken(registry, { env, homeDir });
|
|
243
|
+
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
|
244
|
+
const body = { q: question };
|
|
245
|
+
if (flags.limit !== undefined) body.limit = flags.limit;
|
|
246
|
+
if (flags.categories) body.categories = flags.categories;
|
|
247
|
+
|
|
248
|
+
let response;
|
|
249
|
+
try {
|
|
250
|
+
response = await postJson(url, body, { fetchImpl, headers });
|
|
251
|
+
} catch (error) {
|
|
252
|
+
// A 402 plan_limit is a RegistryError. Do not call the model.
|
|
253
|
+
const message = error instanceof RegistryNetworkError || error instanceof RegistryError
|
|
254
|
+
? error.message
|
|
255
|
+
: 'Could not query Hub.';
|
|
256
|
+
printError(message);
|
|
257
|
+
return { exitCode: 1, registry, calls: 1 };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const hits = verifiedHits(response?.hits);
|
|
261
|
+
const skills = verifiedSkills(response?.skills);
|
|
262
|
+
if (!hits.length && !skills.length) {
|
|
263
|
+
printError('Hub has no matching evidence.');
|
|
264
|
+
return { exitCode: 1, registry, calls: 1 };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (flags.evidence) {
|
|
268
|
+
printEvidence(skills, hits, print);
|
|
269
|
+
return { exitCode: 0, registry, hits, skills, calls: 1 };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const baseUrl = modelBase(flags, env);
|
|
273
|
+
const endpoint = chatCompletionsUrl(baseUrl);
|
|
274
|
+
const apiKey = typeof env.KNOLO_MODEL_API_KEY === 'string' ? env.KNOLO_MODEL_API_KEY.trim() : '';
|
|
275
|
+
const modelHeaders = { 'content-type': 'application/json' };
|
|
276
|
+
if (apiKey) modelHeaders.authorization = `Bearer ${apiKey}`;
|
|
277
|
+
const promptParts = [`Question: ${question}`, ''];
|
|
278
|
+
if (skills.length) {
|
|
279
|
+
promptParts.push(
|
|
280
|
+
'Skills:',
|
|
281
|
+
...skills.map((skill) => `Skill: ${skill.publisher}/${skill.name}\n${skill.description}\n${skill.excerpt}`),
|
|
282
|
+
''
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
if (hits.length) {
|
|
286
|
+
promptParts.push(
|
|
287
|
+
'Excerpts:',
|
|
288
|
+
...hits.map((hit) => `Source: ${citation(hit)}\nHeading: ${hit.heading}\n${hit.excerpt}`)
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const prompt = promptParts.join('\n');
|
|
292
|
+
|
|
293
|
+
let modelResponse;
|
|
294
|
+
try {
|
|
295
|
+
modelResponse = await fetchImpl(endpoint, {
|
|
296
|
+
method: 'POST',
|
|
297
|
+
headers: modelHeaders,
|
|
298
|
+
body: JSON.stringify({
|
|
299
|
+
model,
|
|
300
|
+
messages: [
|
|
301
|
+
{ role: 'system', content: ASK_SYSTEM_PROMPT },
|
|
302
|
+
{ role: 'user', content: prompt },
|
|
303
|
+
],
|
|
304
|
+
}),
|
|
305
|
+
});
|
|
306
|
+
} catch {
|
|
307
|
+
printError('Model endpoint is unavailable. Excerpts follow.');
|
|
308
|
+
printEvidence(skills, hits, print);
|
|
309
|
+
return { exitCode: 2, registry, hits, skills, calls: 2 };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
let modelBody;
|
|
313
|
+
try {
|
|
314
|
+
const text = typeof modelResponse?.text === 'function' ? await modelResponse.text() : '';
|
|
315
|
+
modelBody = text ? JSON.parse(text) : undefined;
|
|
316
|
+
} catch {
|
|
317
|
+
modelBody = undefined;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const answer = modelResponse?.ok ? answerText(modelBody) : '';
|
|
321
|
+
if (!answer) {
|
|
322
|
+
const status = typeof modelResponse?.status === 'number' ? modelResponse.status : 'error';
|
|
323
|
+
printError(`Model endpoint returned HTTP ${status}. Excerpts follow.`);
|
|
324
|
+
printEvidence(skills, hits, print);
|
|
325
|
+
return { exitCode: 2, registry, hits, skills, calls: 2 };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
print(answer);
|
|
329
|
+
print('');
|
|
330
|
+
print('Citations:');
|
|
331
|
+
for (const skill of skills) print(`${skill.publisher}/${skill.name}`);
|
|
332
|
+
for (const hit of hits) print(citation(hit));
|
|
333
|
+
return { exitCode: 0, registry, hits, skills, calls: 2 };
|
|
334
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const KNOWLEDGE_CATEGORIES = [
|
|
2
|
+
'coding',
|
|
3
|
+
'math',
|
|
4
|
+
'science',
|
|
5
|
+
'news',
|
|
6
|
+
'business',
|
|
7
|
+
'security',
|
|
8
|
+
'writing',
|
|
9
|
+
'general',
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const IDS = new Set(KNOWLEDGE_CATEGORIES);
|
|
13
|
+
|
|
14
|
+
export function normalizeCategories(values, { min = 1, max = KNOWLEDGE_CATEGORIES.length } = {}) {
|
|
15
|
+
if (values == null) return null;
|
|
16
|
+
const list = Array.isArray(values) ? values : [values];
|
|
17
|
+
if (list.length === 0) {
|
|
18
|
+
throw new Error(`Choose ${min} to ${max} categories.`);
|
|
19
|
+
}
|
|
20
|
+
const seen = [];
|
|
21
|
+
for (const value of list) {
|
|
22
|
+
if (typeof value !== 'string' || !IDS.has(value)) {
|
|
23
|
+
throw new Error(`--category must be one of ${KNOWLEDGE_CATEGORIES.join(', ')}.`);
|
|
24
|
+
}
|
|
25
|
+
if (!seen.includes(value)) seen.push(value);
|
|
26
|
+
}
|
|
27
|
+
if (seen.length < min || seen.length > max) {
|
|
28
|
+
throw new Error(`Choose ${min} to ${max} categories.`);
|
|
29
|
+
}
|
|
30
|
+
return seen;
|
|
31
|
+
}
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
readArtifact,
|
|
23
23
|
stageCachedArtifact,
|
|
24
24
|
} from './artifacts.mjs';
|
|
25
|
+
import { normalizeCategories } from './categories.mjs';
|
|
25
26
|
import { RegistryError } from './errors.mjs';
|
|
26
27
|
import { getJson, postJson, putBytes } from './http.mjs';
|
|
27
28
|
import {
|
|
@@ -445,6 +446,7 @@ export async function runHubPublish(
|
|
|
445
446
|
...(flags.intendedUse !== undefined
|
|
446
447
|
? { intendedUse: flags.intendedUse }
|
|
447
448
|
: {}),
|
|
449
|
+
...(flags.categories ? { categories: flags.categories } : {}),
|
|
448
450
|
};
|
|
449
451
|
const draftResponse = await postHubJson(
|
|
450
452
|
registryApiUrl(registry, ['publish', 'drafts']),
|
|
@@ -561,6 +563,14 @@ export function parsePublishArgs(args) {
|
|
|
561
563
|
flags.private = true;
|
|
562
564
|
continue;
|
|
563
565
|
}
|
|
566
|
+
if (arg === '--category') {
|
|
567
|
+
const value = args[++i];
|
|
568
|
+
if (value === undefined || value.startsWith('--')) {
|
|
569
|
+
throw new Error('Missing value for --category');
|
|
570
|
+
}
|
|
571
|
+
flags.categories = [...(flags.categories ?? []), value];
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
564
574
|
if (valueFlags.has(arg)) {
|
|
565
575
|
const value = args[++i];
|
|
566
576
|
if (value === undefined || value.startsWith('--'))
|
|
@@ -580,7 +590,7 @@ export function parsePublishArgs(args) {
|
|
|
580
590
|
!flags.license
|
|
581
591
|
) {
|
|
582
592
|
throw new Error(
|
|
583
|
-
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--private] [--json] [--registry <url>]'
|
|
593
|
+
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--category <id>] [--private] [--json] [--registry <url>]'
|
|
584
594
|
);
|
|
585
595
|
}
|
|
586
596
|
if (!/^[a-z0-9-]+$/.test(flags.slug))
|
|
@@ -594,6 +604,9 @@ export function parsePublishArgs(args) {
|
|
|
594
604
|
throw new Error(
|
|
595
605
|
'Publish publisher must contain lowercase letters, numbers, and hyphens.'
|
|
596
606
|
);
|
|
607
|
+
if (flags.categories) {
|
|
608
|
+
flags.categories = normalizeCategories(flags.categories, { min: 1, max: 3 });
|
|
609
|
+
}
|
|
597
610
|
return { packPath: positional[0], flags };
|
|
598
611
|
}
|
|
599
612
|
|
package/bin/research.mjs
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
import { createHash } from 'node:crypto';
|
|
9
9
|
import { existsSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
-
import { fileURLToPath
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { loadCoreModule } from './load-core.mjs';
|
|
12
13
|
import {
|
|
13
14
|
KAR_CLI_MAX_IMAGE_BYTES,
|
|
14
15
|
KAR_CLI_MAX_TEXT_BYTES,
|
|
@@ -512,18 +513,12 @@ async function loadApis() {
|
|
|
512
513
|
}
|
|
513
514
|
|
|
514
515
|
async function importFirst(candidates, check) {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const loaded = await import(href);
|
|
521
|
-
if (check(loaded)) return loaded;
|
|
522
|
-
} catch {
|
|
523
|
-
// Try the next candidate.
|
|
524
|
-
}
|
|
516
|
+
try {
|
|
517
|
+
return await loadCoreModule(candidates, check);
|
|
518
|
+
} catch (error) {
|
|
519
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
520
|
+
throw new Error(`Could not load @knolo/core. Build packages/core first. (${detail})`);
|
|
525
521
|
}
|
|
526
|
-
throw new Error('Could not load @knolo/core. Build packages/core first.');
|
|
527
522
|
}
|
|
528
523
|
|
|
529
524
|
function parseVerifyFlags(argv) {
|
package/bin/skill.mjs
CHANGED
|
@@ -21,7 +21,9 @@ import {
|
|
|
21
21
|
} from 'node:fs/promises';
|
|
22
22
|
import { homedir, tmpdir } from 'node:os';
|
|
23
23
|
import path from 'node:path';
|
|
24
|
-
import { fileURLToPath
|
|
24
|
+
import { fileURLToPath } from 'node:url';
|
|
25
|
+
import { loadCoreModule } from './load-core.mjs';
|
|
26
|
+
import { normalizeCategories } from './registry/categories.mjs';
|
|
25
27
|
import {
|
|
26
28
|
normalizeRegistryUrl,
|
|
27
29
|
registryApiUrl,
|
|
@@ -44,7 +46,7 @@ const USAGE = `Usage: knolo skill <init|check|pack|publish|install|search|info|l
|
|
|
44
46
|
knolo skill init <name> [--dir <parent>]
|
|
45
47
|
knolo skill check [<dir>] [--query <text>] [--json]
|
|
46
48
|
knolo skill pack [<dir>] [--out <dir>] [--json]
|
|
47
|
-
knolo skill publish <pack> [--registry <url>] [--json]
|
|
49
|
+
knolo skill publish <pack> [--category <id>] [--registry <url>] [--json]
|
|
48
50
|
knolo skill publish <pack> --hub <dir> [--json]
|
|
49
51
|
knolo skill install <publisher>/<name> [--dir <path>] [--registry <url>] [--json]
|
|
50
52
|
knolo skill install <name> --hub <dir> [--dir <path>] [--json]
|
|
@@ -216,6 +218,14 @@ async function packSkill(dirArg, flags) {
|
|
|
216
218
|
|
|
217
219
|
async function publishSkill(packArg, flags) {
|
|
218
220
|
if (!packArg) throw new Error('SKILL_HUB: pass the pack directory.');
|
|
221
|
+
const categories = flags.category
|
|
222
|
+
? normalizeCategories(flags.category, { min: 1, max: 3 })
|
|
223
|
+
: null;
|
|
224
|
+
if (!flags.hub && !categories) {
|
|
225
|
+
throw new Error(
|
|
226
|
+
'Pass --category at least once. Choose 1 to 3 of coding, math, science, news, business, security, writing, general.'
|
|
227
|
+
);
|
|
228
|
+
}
|
|
219
229
|
if (flags.hub) {
|
|
220
230
|
if (flags.registry) {
|
|
221
231
|
throw new Error(
|
|
@@ -228,7 +238,7 @@ async function publishSkill(packArg, flags) {
|
|
|
228
238
|
const registry = resolveSkillRegistry(flags.registry, credentials.registry);
|
|
229
239
|
const summary = await publishSkillToRegistry(
|
|
230
240
|
path.resolve(process.cwd(), packArg),
|
|
231
|
-
{ registry, token: credentials.token }
|
|
241
|
+
{ registry, token: credentials.token, categories }
|
|
232
242
|
);
|
|
233
243
|
if (flags.json) console.log(JSON.stringify(summary));
|
|
234
244
|
else if (summary.unchanged) console.log(`unchanged ${summary.name}`);
|
|
@@ -895,32 +905,19 @@ function coreModuleReady(loaded) {
|
|
|
895
905
|
);
|
|
896
906
|
}
|
|
897
907
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
if (coreModuleReady(loaded)) return loaded;
|
|
912
|
-
lastError = new Error('module did not export the Knowledge Image API');
|
|
913
|
-
} catch (error) {
|
|
914
|
-
lastError = error;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
if (attempt < 2) {
|
|
918
|
-
await new Promise((resolve) => setTimeout(resolve, 25 * (attempt + 1)));
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
throw new Error(
|
|
922
|
-
`Could not load @knolo/core. Build packages/core first. (${coreLoadDetail(lastError)})`
|
|
923
|
-
);
|
|
908
|
+
let corePromise;
|
|
909
|
+
|
|
910
|
+
function loadCore() {
|
|
911
|
+
corePromise ??= loadCoreModule(
|
|
912
|
+
[path.resolve(__dirname, '../../core/dist/index.js'), '@knolo/core'],
|
|
913
|
+
coreModuleReady
|
|
914
|
+
).catch((error) => {
|
|
915
|
+
corePromise = undefined;
|
|
916
|
+
throw new Error(
|
|
917
|
+
`Could not load @knolo/core. Build packages/core first. (${coreLoadDetail(error)})`
|
|
918
|
+
);
|
|
919
|
+
});
|
|
920
|
+
return corePromise;
|
|
924
921
|
}
|
|
925
922
|
|
|
926
923
|
const SKILL_ARCHIVE_FORMAT = 'knolo-skill-archive-v1';
|
|
@@ -982,8 +979,14 @@ export function decodeSkillArchive(bytes) {
|
|
|
982
979
|
|
|
983
980
|
export async function publishSkillToRegistry(
|
|
984
981
|
packDir,
|
|
985
|
-
{ fetchImpl = globalThis.fetch, registry, token }
|
|
982
|
+
{ fetchImpl = globalThis.fetch, registry, token, categories }
|
|
986
983
|
) {
|
|
984
|
+
const shelf = normalizeCategories(categories, { min: 1, max: 3 });
|
|
985
|
+
if (!shelf) {
|
|
986
|
+
throw new Error(
|
|
987
|
+
'Pass --category at least once. Choose 1 to 3 of coding, math, science, news, business, security, writing, general.'
|
|
988
|
+
);
|
|
989
|
+
}
|
|
987
990
|
const pack = await readVerifiedPack(packDir);
|
|
988
991
|
const archive = encodeSkillArchive(pack);
|
|
989
992
|
const archiveSha256 = sha256(archive);
|
|
@@ -1016,7 +1019,7 @@ export async function publishSkillToRegistry(
|
|
|
1016
1019
|
try {
|
|
1017
1020
|
listed = await postJson(
|
|
1018
1021
|
registryApiUrl(registry, ['skills']),
|
|
1019
|
-
{ archiveSha256, url: blobUrl, sizeBytes: archive.length },
|
|
1022
|
+
{ archiveSha256, url: blobUrl, sizeBytes: archive.length, categories: shelf },
|
|
1020
1023
|
{ fetchImpl, headers: auth }
|
|
1021
1024
|
);
|
|
1022
1025
|
} catch (error) {
|
|
@@ -1360,7 +1363,15 @@ function parseArgs(argv) {
|
|
|
1360
1363
|
const arg = argv[index];
|
|
1361
1364
|
if (arg === '--json') flags.json = true;
|
|
1362
1365
|
else if (arg === '--help' || arg === '-h') flags.help = true;
|
|
1363
|
-
else if (arg
|
|
1366
|
+
else if (arg === '--category') {
|
|
1367
|
+
const value = argv[index + 1];
|
|
1368
|
+
if (value === undefined || value.startsWith('--')) {
|
|
1369
|
+
throw new Error('Missing value for --category');
|
|
1370
|
+
}
|
|
1371
|
+
const current = Array.isArray(flags.category) ? flags.category : [];
|
|
1372
|
+
flags.category = [...current, value];
|
|
1373
|
+
index += 1;
|
|
1374
|
+
} else if (arg.startsWith('--')) {
|
|
1364
1375
|
const key = arg.slice(2);
|
|
1365
1376
|
const value = argv[index + 1];
|
|
1366
1377
|
if (value === undefined || value.startsWith('--')) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knolo/cli",
|
|
3
|
-
"version": "6.2.
|
|
3
|
+
"version": "6.2.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"knolo": "bin/knolo.mjs"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "node -e \"console.log('cli is executable via bin/knolo.mjs')\"",
|
|
31
|
-
"test": "npm run build --prefix ../core && npm run build --prefix ../exchange && node --test test/cli.test.mjs test/kar.test.mjs test/kar-graph.test.mjs test/kar-produce.test.mjs test/hub.integration.test.mjs test/compose.test.mjs test/cortex.test.mjs test/flight.test.mjs test/skill.test.mjs test/setup.test.mjs test/exchange.test.mjs test/research.test.mjs test/kite.test.mjs",
|
|
31
|
+
"test": "npm run build --prefix ../core && npm run build --prefix ../exchange && node --test test/cli.test.mjs test/ask.test.mjs test/kar.test.mjs test/kar-graph.test.mjs test/kar-produce.test.mjs test/hub.integration.test.mjs test/compose.test.mjs test/cortex.test.mjs test/flight.test.mjs test/skill.test.mjs test/persona.test.mjs test/setup.test.mjs test/exchange.test.mjs test/research.test.mjs test/kite.test.mjs",
|
|
32
32
|
"test:hub": "node --test test/hub.integration.test.mjs"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|