@introspection-ai/recipes 0.38.0 → 0.40.1
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 +10 -4
- package/dist/channels/index.d.ts +2 -2
- package/dist/channels/index.d.ts.map +1 -1
- package/dist/channels/index.js +1 -1
- package/dist/channels/index.js.map +1 -1
- package/dist/channels/module.d.ts +18 -0
- package/dist/channels/module.d.ts.map +1 -1
- package/dist/channels/module.js +6 -0
- package/dist/channels/module.js.map +1 -1
- package/dist/child/session.d.ts +2 -0
- package/dist/child/session.d.ts.map +1 -1
- package/dist/child/session.js +3 -0
- package/dist/child/session.js.map +1 -1
- package/dist/connector-tools.d.ts +6 -0
- package/dist/connector-tools.d.ts.map +1 -1
- package/dist/connector-tools.js +26 -11
- package/dist/connector-tools.js.map +1 -1
- package/dist/documents/index.d.ts +4 -2
- package/dist/documents/index.d.ts.map +1 -1
- package/dist/documents/index.js +2 -1
- package/dist/documents/index.js.map +1 -1
- package/dist/documents/module.d.ts +44 -11
- package/dist/documents/module.d.ts.map +1 -1
- package/dist/documents/module.js +35 -24
- package/dist/documents/module.js.map +1 -1
- package/dist/documents/python.d.ts +30 -0
- package/dist/documents/python.d.ts.map +1 -0
- package/dist/documents/python.js +64 -0
- package/dist/documents/python.js.map +1 -0
- package/dist/extensions.d.ts +16 -0
- package/dist/extensions.d.ts.map +1 -1
- package/dist/extensions.js +12 -0
- package/dist/extensions.js.map +1 -1
- package/dist/mcp-chunks/{chunk-EQCSXVQ7.js → chunk-EZKOZO3F.js} +5184 -1309
- package/dist/mcp-daemon.js +1 -1
- package/dist/mcp-run-worker.js +1 -1
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +89 -14
- package/dist/pi-extension.js.map +1 -1
- package/dist/python.d.ts +56 -0
- package/dist/python.d.ts.map +1 -0
- package/dist/python.js +100 -0
- package/dist/python.js.map +1 -0
- package/dist/recipe/companions.d.ts +68 -0
- package/dist/recipe/companions.d.ts.map +1 -0
- package/dist/recipe/companions.js +149 -0
- package/dist/recipe/companions.js.map +1 -0
- package/dist/recipe/extensions.d.ts +6 -0
- package/dist/recipe/extensions.d.ts.map +1 -1
- package/dist/recipe/extensions.js +51 -14
- package/dist/recipe/extensions.js.map +1 -1
- package/dist/recipe/package.d.ts +72 -9
- package/dist/recipe/package.d.ts.map +1 -1
- package/dist/recipe/package.js +261 -76
- package/dist/recipe/package.js.map +1 -1
- package/dist/recipe/python.d.ts +152 -0
- package/dist/recipe/python.d.ts.map +1 -0
- package/dist/recipe/python.js +708 -0
- package/dist/recipe/python.js.map +1 -0
- package/dist/recipe/resolve.d.ts +3 -0
- package/dist/recipe/resolve.d.ts.map +1 -1
- package/dist/recipe/resolve.js +32 -16
- package/dist/recipe/resolve.js.map +1 -1
- package/dist/recipe/skills.d.ts +15 -1
- package/dist/recipe/skills.d.ts.map +1 -1
- package/dist/recipe/skills.js +26 -9
- package/dist/recipe/skills.js.map +1 -1
- package/dist/run-controller.d.ts +2 -0
- package/dist/run-controller.d.ts.map +1 -1
- package/dist/run-controller.js +3 -0
- package/dist/run-controller.js.map +1 -1
- package/dist/session.d.ts +8 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +37 -15
- package/dist/session.js.map +1 -1
- package/docs/agent-composition.md +2 -2
- package/docs/channels.md +55 -11
- package/docs/documents.md +109 -27
- package/docs/interactions.md +1 -1
- package/docs/pi-extension.md +4 -1
- package/docs/recipe-format.md +262 -62
- package/docs/slack.md +4 -5
- package/package.json +17 -7
package/dist/recipe/package.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { existsSync, realpathSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
|
-
import {
|
|
2
|
+
import { createRequire, findPackageJSON } from "node:module";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { minimatch } from "minimatch";
|
|
3
5
|
import { documentProfileShapeProblem, isDocumentProfilePath, recipeProfileSkill } from "../documents/profile.js";
|
|
6
|
+
import { isCompanionPackage, isHostPeerPackage } from "./companions.js";
|
|
4
7
|
const INVALID_MCP_TOOL_SELECTION = Symbol("invalidMcpToolSelection");
|
|
5
8
|
/** Parse a selection while preserving whether its source shape was malformed. */
|
|
6
9
|
export function parseRecipeMcpToolSelection(value) {
|
|
@@ -57,6 +60,10 @@ export function documentProfileProblem(profile, packageDir) {
|
|
|
57
60
|
export function recipeDocumentPackageName(format) {
|
|
58
61
|
return `@introspection-ai/recipe-documents-${format}`;
|
|
59
62
|
}
|
|
63
|
+
/** The findings that make a report invalid: every finding but warnings. */
|
|
64
|
+
export function recipeValidationErrors(findings) {
|
|
65
|
+
return findings.filter((item) => item.severity !== "warning");
|
|
66
|
+
}
|
|
60
67
|
const RESOURCE_KEYS = [
|
|
61
68
|
"agents",
|
|
62
69
|
"extensions",
|
|
@@ -90,6 +97,47 @@ function emptyResources() {
|
|
|
90
97
|
function resourceDeclarations(pi) {
|
|
91
98
|
return Object.fromEntries(RESOURCE_KEYS.map((key) => [key, Object.hasOwn(pi, key)]));
|
|
92
99
|
}
|
|
100
|
+
function versioned(value) {
|
|
101
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Why a `pi.channels` or `pi.documents` declaration's package (`packageName`)
|
|
105
|
+
* is declared wrongly, or undefined when it is fine. Listed in `dependencies`
|
|
106
|
+
* with a version, the Recipe brings and pins it; listed in `peerDependencies`,
|
|
107
|
+
* the platform provides it (see `peerFindings`). Omitted entirely, only a host
|
|
108
|
+
* default is supplied; every other package must be declared.
|
|
109
|
+
*/
|
|
110
|
+
function declarationPackageProblem(declarationName, packageName, declared) {
|
|
111
|
+
const dependencies = asRecord(declared.dependencies);
|
|
112
|
+
const required = `${declarationName} requires dependency '${packageName}'`;
|
|
113
|
+
if (Object.hasOwn(dependencies, packageName)) {
|
|
114
|
+
return versioned(dependencies[packageName]) ? undefined : required;
|
|
115
|
+
}
|
|
116
|
+
if (Object.hasOwn(asRecord(declared.peerDependencies), packageName))
|
|
117
|
+
return undefined;
|
|
118
|
+
return isCompanionPackage(packageName) ? undefined : required;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A peer means the platform running the Recipe provides it: the managed
|
|
122
|
+
* install never installs peers. Each needs a version range; one that is
|
|
123
|
+
* neither a package the host imports (Recipes' own peers) nor a companion package
|
|
124
|
+
* is a warning, since a standard platform does not provide it.
|
|
125
|
+
*/
|
|
126
|
+
function peerFindings(declared, packageName) {
|
|
127
|
+
const findings = [];
|
|
128
|
+
for (const [name, version] of Object.entries(asRecord(declared.peerDependencies))) {
|
|
129
|
+
if (!versioned(version)) {
|
|
130
|
+
findings.push(finding("package.peer_version_missing", `package.json#peerDependencies '${name}' needs a version range`, packageName));
|
|
131
|
+
}
|
|
132
|
+
else if (!isHostPeerPackage(name) && !isCompanionPackage(name)) {
|
|
133
|
+
findings.push({
|
|
134
|
+
...finding("package.peer_not_provided_by_default", `package.json#peerDependencies '${name}' is not provided by a standard platform: the platform running the Recipe must provide it, or the runtime image build fails. Add it to dependencies to bring it with the Recipe`, packageName),
|
|
135
|
+
severity: "warning",
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return findings;
|
|
140
|
+
}
|
|
93
141
|
function sourceShapeFindings(pi, packageName, dependencies, packageDir) {
|
|
94
142
|
const findings = [];
|
|
95
143
|
for (const key of Object.keys(pi)) {
|
|
@@ -113,6 +161,7 @@ function sourceShapeFindings(pi, packageName, dependencies, packageDir) {
|
|
|
113
161
|
findings.push(...documentSourceShapeFindings(pi.documents, packageName, dependencies, packageDir));
|
|
114
162
|
findings.push(...mcpSourceShapeFindings(pi.mcp, packageName));
|
|
115
163
|
findings.push(...runtimeSourceShapeFindings(pi.runtime, packageName));
|
|
164
|
+
findings.push(...peerFindings(dependencies, packageName));
|
|
116
165
|
return findings;
|
|
117
166
|
}
|
|
118
167
|
function channelSourceShapeFindings(value, packageName, dependencies) {
|
|
@@ -155,17 +204,9 @@ function channelSourceShapeFindings(value, packageName, dependencies) {
|
|
|
155
204
|
providers.add(provider);
|
|
156
205
|
}
|
|
157
206
|
if (provider) {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
!Array.isArray(dependencies)
|
|
162
|
-
? dependencies
|
|
163
|
-
: {};
|
|
164
|
-
const dependencyVersion = declaredDependencies[channelPackage];
|
|
165
|
-
if (typeof dependencyVersion !== "string" ||
|
|
166
|
-
!dependencyVersion.trim()) {
|
|
167
|
-
findings.push(invalid(`package.json#pi.channels provider '${provider ?? "unknown"}' requires dependency '${channelPackage}'`));
|
|
168
|
-
}
|
|
207
|
+
const problem = declarationPackageProblem(`package.json#pi.channels provider '${provider}'`, recipeChannelPackageName(provider), dependencies);
|
|
208
|
+
if (problem)
|
|
209
|
+
findings.push(invalid(problem));
|
|
169
210
|
}
|
|
170
211
|
}
|
|
171
212
|
return findings;
|
|
@@ -177,9 +218,6 @@ function documentSourceShapeFindings(value, packageName, dependencies, packageDi
|
|
|
177
218
|
if (!Array.isArray(value)) {
|
|
178
219
|
return [invalid("package.json#pi.documents must be an array")];
|
|
179
220
|
}
|
|
180
|
-
const declaredDependencies = dependencies && typeof dependencies === "object" && !Array.isArray(dependencies)
|
|
181
|
-
? dependencies
|
|
182
|
-
: {};
|
|
183
221
|
const findings = [];
|
|
184
222
|
const formats = new Set();
|
|
185
223
|
for (const [index, raw] of value.entries()) {
|
|
@@ -212,14 +250,17 @@ function documentSourceShapeFindings(value, packageName, dependencies, packageDi
|
|
|
212
250
|
continue;
|
|
213
251
|
}
|
|
214
252
|
formats.add(format);
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
findings.push(invalid(`package.json#pi.documents format '${format}' requires dependency '${documentPackage}'`));
|
|
219
|
-
}
|
|
253
|
+
const problem = declarationPackageProblem(`package.json#pi.documents format '${format}'`, recipeDocumentPackageName(format), dependencies);
|
|
254
|
+
if (problem)
|
|
255
|
+
findings.push(invalid(problem));
|
|
220
256
|
}
|
|
221
257
|
return findings;
|
|
222
258
|
}
|
|
259
|
+
const PYTHON_MODULE_NAME = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
260
|
+
/** Whether `value` is a dotted Python module name such as `docx` or `paper_docx.api`. */
|
|
261
|
+
export function isPythonModuleName(value) {
|
|
262
|
+
return PYTHON_MODULE_NAME.test(value);
|
|
263
|
+
}
|
|
223
264
|
function runtimeSourceShapeFindings(value, packageName) {
|
|
224
265
|
if (value === undefined)
|
|
225
266
|
return [];
|
|
@@ -230,7 +271,7 @@ function runtimeSourceShapeFindings(value, packageName) {
|
|
|
230
271
|
const runtime = value;
|
|
231
272
|
const findings = [];
|
|
232
273
|
for (const key of Object.keys(runtime)) {
|
|
233
|
-
if (
|
|
274
|
+
if (key !== "python") {
|
|
234
275
|
findings.push(invalid(`package.json#pi.runtime contains unknown field '${key}'`));
|
|
235
276
|
}
|
|
236
277
|
}
|
|
@@ -259,34 +300,10 @@ function runtimeSourceShapeFindings(value, packageName) {
|
|
|
259
300
|
python.imports.some((item) => typeof item !== "string" || !item.trim()))) {
|
|
260
301
|
findings.push(invalid("package.json#pi.runtime.python.imports must be an array of non-empty module names"));
|
|
261
302
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
findings.push(invalid("package.json#pi.runtime.system must be an object"));
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
const system = runtime.system;
|
|
270
|
-
for (const key of Object.keys(system)) {
|
|
271
|
-
if (key !== "packages") {
|
|
272
|
-
findings.push(invalid(`package.json#pi.runtime.system contains unknown field '${key}'`));
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (!Array.isArray(system.packages)) {
|
|
276
|
-
findings.push(invalid("package.json#pi.runtime.system.packages must be an array"));
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
for (const [index, entry] of system.packages.entries()) {
|
|
280
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
281
|
-
findings.push(invalid(`package.json#pi.runtime.system.packages[${index}] must be an object`));
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
const pkg = entry;
|
|
285
|
-
if (typeof pkg.id !== "string" ||
|
|
286
|
-
!pkg.id.trim() ||
|
|
287
|
-
typeof pkg.version !== "string" ||
|
|
288
|
-
!pkg.version.trim()) {
|
|
289
|
-
findings.push(invalid(`package.json#pi.runtime.system.packages[${index}] requires non-empty id and version`));
|
|
303
|
+
else if (Array.isArray(python.imports)) {
|
|
304
|
+
for (const item of python.imports) {
|
|
305
|
+
if (!isPythonModuleName(item.trim())) {
|
|
306
|
+
findings.push(invalid(`package.json#pi.runtime.python.imports entry '${item}' must be a dotted Python module name`));
|
|
290
307
|
}
|
|
291
308
|
}
|
|
292
309
|
}
|
|
@@ -389,28 +406,32 @@ function hasTraversalSegment(path) {
|
|
|
389
406
|
.split("/")
|
|
390
407
|
.some((segment) => segment === "." || segment === "..");
|
|
391
408
|
}
|
|
392
|
-
function assertPackageResourcePath(pkg, key, resource, resolved) {
|
|
409
|
+
function assertPackageResourcePath(pkg, key, resource, resolved, owner = `Recipe ${pkg.name}`) {
|
|
393
410
|
if (isAbsolute(resource) || hasTraversalSegment(resource)) {
|
|
394
|
-
throw new RecipePackageError(
|
|
411
|
+
throw new RecipePackageError(`${owner} declares ${key} resource outside the package: ${resource}`);
|
|
395
412
|
}
|
|
396
413
|
const relativePath = relative(resolve(pkg.path), resolved);
|
|
397
414
|
if (relativePath === ".." ||
|
|
398
415
|
relativePath.startsWith("../") ||
|
|
399
416
|
relativePath.startsWith("..\\") ||
|
|
400
417
|
isAbsolute(relativePath)) {
|
|
401
|
-
throw new RecipePackageError(
|
|
418
|
+
throw new RecipePackageError(`${owner} declares ${key} resource outside the package: ${resource}`);
|
|
402
419
|
}
|
|
403
420
|
}
|
|
404
|
-
|
|
421
|
+
/**
|
|
422
|
+
* Throw unless `path`'s real path is inside `packageDir`'s real path. `owner`
|
|
423
|
+
* starts the message; a dependency package passes `Dependency <name>`.
|
|
424
|
+
*/
|
|
425
|
+
export function assertRecipePathContained(packageDir, path, label, owner = "Recipe") {
|
|
405
426
|
const root = realpathSync(packageDir);
|
|
406
427
|
const target = realpathSync(path);
|
|
407
428
|
const relativePath = relative(root, target);
|
|
408
429
|
if (relativePath === ".." || relativePath.startsWith(`..${sep}`) || isAbsolute(relativePath)) {
|
|
409
|
-
throw new RecipePackageError(
|
|
430
|
+
throw new RecipePackageError(`${owner} ${label} resolves outside the package: ${path}`);
|
|
410
431
|
}
|
|
411
432
|
}
|
|
412
|
-
function assertResourceTreeContained(packageDir, path, label, seen = new Set()) {
|
|
413
|
-
assertRecipePathContained(packageDir, path, label);
|
|
433
|
+
function assertResourceTreeContained(packageDir, path, label, owner = "Recipe", seen = new Set()) {
|
|
434
|
+
assertRecipePathContained(packageDir, path, label, owner);
|
|
414
435
|
const realPath = realpathSync(path);
|
|
415
436
|
if (seen.has(realPath))
|
|
416
437
|
return;
|
|
@@ -419,9 +440,9 @@ function assertResourceTreeContained(packageDir, path, label, seen = new Set())
|
|
|
419
440
|
return;
|
|
420
441
|
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
421
442
|
const child = join(path, entry.name);
|
|
422
|
-
assertRecipePathContained(packageDir, child, label);
|
|
443
|
+
assertRecipePathContained(packageDir, child, label, owner);
|
|
423
444
|
if (statSync(child).isDirectory()) {
|
|
424
|
-
assertResourceTreeContained(packageDir, child, label, seen);
|
|
445
|
+
assertResourceTreeContained(packageDir, child, label, owner, seen);
|
|
425
446
|
}
|
|
426
447
|
}
|
|
427
448
|
}
|
|
@@ -608,7 +629,6 @@ function parseDocuments(value) {
|
|
|
608
629
|
function parseRuntimeRequirements(value) {
|
|
609
630
|
const runtime = asRecord(value);
|
|
610
631
|
const python = asRecord(runtime.python);
|
|
611
|
-
const system = asRecord(runtime.system);
|
|
612
632
|
return {
|
|
613
633
|
...(stringValue(python.project) && stringValue(python.lockfile)
|
|
614
634
|
? {
|
|
@@ -620,15 +640,6 @@ function parseRuntimeRequirements(value) {
|
|
|
620
640
|
},
|
|
621
641
|
}
|
|
622
642
|
: {}),
|
|
623
|
-
system: {
|
|
624
|
-
packages: (Array.isArray(system.packages) ? system.packages : [])
|
|
625
|
-
.map(asRecord)
|
|
626
|
-
.filter((item) => stringValue(item.id) && stringValue(item.version))
|
|
627
|
-
.map((item) => ({
|
|
628
|
-
id: stringValue(item.id),
|
|
629
|
-
version: stringValue(item.version),
|
|
630
|
-
})),
|
|
631
|
-
},
|
|
632
643
|
};
|
|
633
644
|
}
|
|
634
645
|
function packageJsonPath(packageDir) {
|
|
@@ -674,7 +685,7 @@ export function readPiPackageManifest(packageDir) {
|
|
|
674
685
|
runtime: parseRuntimeRequirements(pi.runtime),
|
|
675
686
|
};
|
|
676
687
|
Object.defineProperty(manifest, SOURCE_FINDINGS, {
|
|
677
|
-
value: sourceShapeFindings(pi, manifest.name, raw.dependencies, packageDir),
|
|
688
|
+
value: sourceShapeFindings(pi, manifest.name, { dependencies: raw.dependencies, peerDependencies: raw.peerDependencies }, packageDir),
|
|
678
689
|
enumerable: false,
|
|
679
690
|
});
|
|
680
691
|
return manifest;
|
|
@@ -726,7 +737,14 @@ export function resolvePiPackageMcpManifestPaths(pkg) {
|
|
|
726
737
|
}
|
|
727
738
|
return resolved;
|
|
728
739
|
}
|
|
729
|
-
|
|
740
|
+
/**
|
|
741
|
+
* Resolve a package's declared resources of one kind. `owner` names the
|
|
742
|
+
* package in errors: the Recipe by default, `Dependency <name>` for a
|
|
743
|
+
* dependency package.
|
|
744
|
+
*/
|
|
745
|
+
export function resolvePiPackageResourcePaths(pkg, key, owner) {
|
|
746
|
+
const declarer = owner ?? `Recipe ${pkg.name}`;
|
|
747
|
+
const container = owner ?? "Recipe";
|
|
730
748
|
const globs = pkg.resources[key];
|
|
731
749
|
const resolved = [];
|
|
732
750
|
const seen = new Set();
|
|
@@ -740,13 +758,13 @@ export function resolvePiPackageResourcePaths(pkg, key) {
|
|
|
740
758
|
for (const glob of globs) {
|
|
741
759
|
if (!glob.trim())
|
|
742
760
|
continue;
|
|
743
|
-
assertPackageResourcePath(pkg, key, glob, resolve(pkg.path, glob));
|
|
761
|
+
assertPackageResourcePath(pkg, key, glob, resolve(pkg.path, glob), declarer);
|
|
744
762
|
if (!hasGlob(glob)) {
|
|
745
763
|
const direct = resolve(pkg.path, glob);
|
|
746
764
|
if (!existsSync(direct)) {
|
|
747
|
-
throw new RecipePackageError(
|
|
765
|
+
throw new RecipePackageError(`${declarer} declares missing ${key} resource: ${glob}`);
|
|
748
766
|
}
|
|
749
|
-
assertResourceTreeContained(pkg.path, direct, `${key} resource
|
|
767
|
+
assertResourceTreeContained(pkg.path, direct, `${key} resource`, container);
|
|
750
768
|
add(direct);
|
|
751
769
|
continue;
|
|
752
770
|
}
|
|
@@ -755,10 +773,10 @@ export function resolvePiPackageResourcePaths(pkg, key) {
|
|
|
755
773
|
.filter((entry) => matcher.test(normalizeResourcePath(entry)))
|
|
756
774
|
.map((entry) => resolve(pkg.path, entry));
|
|
757
775
|
if (matches.length === 0) {
|
|
758
|
-
throw new RecipePackageError(
|
|
776
|
+
throw new RecipePackageError(`${declarer} declares ${key} glob with no matches: ${glob}`);
|
|
759
777
|
}
|
|
760
778
|
for (const match of matches.sort()) {
|
|
761
|
-
assertResourceTreeContained(pkg.path, match, `${key} resource
|
|
779
|
+
assertResourceTreeContained(pkg.path, match, `${key} resource`, container);
|
|
762
780
|
add(match);
|
|
763
781
|
}
|
|
764
782
|
}
|
|
@@ -817,8 +835,175 @@ export function validatePiPackageManifest(pkg) {
|
|
|
817
835
|
}
|
|
818
836
|
}
|
|
819
837
|
return {
|
|
820
|
-
valid: findings.length === 0,
|
|
838
|
+
valid: recipeValidationErrors(findings).length === 0,
|
|
821
839
|
findings,
|
|
822
840
|
};
|
|
823
841
|
}
|
|
842
|
+
/** Whether npm's `files` list ships `path` (a package-relative POSIX path). */
|
|
843
|
+
function packageFilesInclude(files, path) {
|
|
844
|
+
let included = false;
|
|
845
|
+
for (const raw of files) {
|
|
846
|
+
if (typeof raw !== "string" || !raw.trim())
|
|
847
|
+
continue;
|
|
848
|
+
const negated = raw.startsWith("!");
|
|
849
|
+
const entry = normalizeResourcePath(negated ? raw.slice(1) : raw).replace(/\/+$/, "");
|
|
850
|
+
// npm reads `files` with minimatch, so braces and classes expand here too.
|
|
851
|
+
const matches = (candidate) => minimatch(candidate, entry, { dot: true });
|
|
852
|
+
const segments = path.split("/");
|
|
853
|
+
// An entry naming a directory ships everything beneath it.
|
|
854
|
+
const hit = segments.some((_, index) => matches(segments.slice(0, index + 1).join("/")));
|
|
855
|
+
if (hit)
|
|
856
|
+
included = !negated;
|
|
857
|
+
}
|
|
858
|
+
return included;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Validate the `pi.runtime.python` a dependency package (not the Recipe)
|
|
862
|
+
* declares: the same shape a Recipe's takes, a project directory holding
|
|
863
|
+
* `pyproject.toml` and its `uv.lock` inside the package, and, when the package
|
|
864
|
+
* lists `files`, both files shipped in it. Returns no requirement when the
|
|
865
|
+
* package declares none.
|
|
866
|
+
*/
|
|
867
|
+
export function validateRecipePythonPackage(packageDir) {
|
|
868
|
+
const packagePath = packageJsonPath(packageDir);
|
|
869
|
+
if (!packagePath) {
|
|
870
|
+
throw new RecipePackageError(`Package at ${packageDir} is missing package.json`);
|
|
871
|
+
}
|
|
872
|
+
const raw = readJsonFile(packagePath);
|
|
873
|
+
const name = stringValue(raw.name) ?? packageDir;
|
|
874
|
+
const pi = piPackageManifestBlock(raw);
|
|
875
|
+
const runtime = pi ? asRecord(pi.runtime) : {};
|
|
876
|
+
if (runtime.python === undefined)
|
|
877
|
+
return { valid: true, findings: [], name };
|
|
878
|
+
const findings = runtimeSourceShapeFindings({ python: runtime.python }, name);
|
|
879
|
+
const python = parseRuntimeRequirements({ python: runtime.python }).python;
|
|
880
|
+
if (findings.length === 0 && python) {
|
|
881
|
+
const invalidPath = (message) => findings.push(finding("pi.runtime_path_invalid", message, name));
|
|
882
|
+
const contained = (authored) => {
|
|
883
|
+
const rel = relative(resolve(packageDir), resolve(packageDir, authored));
|
|
884
|
+
return !(isAbsolute(authored) ||
|
|
885
|
+
hasTraversalSegment(authored) ||
|
|
886
|
+
rel === ".." ||
|
|
887
|
+
rel.startsWith(`..${sep}`) ||
|
|
888
|
+
isAbsolute(rel));
|
|
889
|
+
};
|
|
890
|
+
const projectDir = resolve(packageDir, python.project);
|
|
891
|
+
const pyproject = `${python.project}/pyproject.toml`.replace(/^\.\//, "");
|
|
892
|
+
if (!contained(python.project) || !existsSync(projectDir) || !statSync(projectDir).isDirectory()) {
|
|
893
|
+
invalidPath(`package.json#pi.runtime.python.project must resolve to a directory inside the package`);
|
|
894
|
+
}
|
|
895
|
+
else if (!existsSync(join(projectDir, "pyproject.toml"))) {
|
|
896
|
+
invalidPath(`package.json#pi.runtime.python.project has no pyproject.toml: ${python.project}`);
|
|
897
|
+
}
|
|
898
|
+
const lockfile = resolve(packageDir, python.lockfile);
|
|
899
|
+
if (!contained(python.lockfile) || !existsSync(lockfile) || !statSync(lockfile).isFile()) {
|
|
900
|
+
invalidPath(`package.json#pi.runtime.python.lockfile must resolve to a file inside the package`);
|
|
901
|
+
}
|
|
902
|
+
else if (lockfile !== join(projectDir, "uv.lock")) {
|
|
903
|
+
invalidPath(`package.json#pi.runtime.python.lockfile must be the project's uv.lock: ${python.lockfile}`);
|
|
904
|
+
}
|
|
905
|
+
if (Array.isArray(raw.files)) {
|
|
906
|
+
for (const shipped of [pyproject, normalizeResourcePath(python.lockfile)]) {
|
|
907
|
+
if (!packageFilesInclude(raw.files, shipped)) {
|
|
908
|
+
findings.push(finding("pi.runtime_files_missing", `package.json#files must ship ${shipped}, which pi.runtime.python needs`, name));
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return { valid: findings.length === 0, findings, name, ...(python ? { python } : {}) };
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* The directory of `name` as Node resolves it from `recipeDir`: the first
|
|
917
|
+
* `node_modules/<name>/package.json` walking up from the Recipe's real path,
|
|
918
|
+
* so a dependency hoisted beside the Recipe (npm, npx, `npm install --prefix`)
|
|
919
|
+
* is found as well as one nested inside it or linked by pnpm. A package with
|
|
920
|
+
* no entry point (a skills-only package) still resolves, because this reads
|
|
921
|
+
* Node's lookup folders rather than resolving an entry. Falls back to
|
|
922
|
+
* `findPackageJSON` for anything only Node's resolver can see.
|
|
923
|
+
*/
|
|
924
|
+
export function resolveDependencyDir(recipeDir, name) {
|
|
925
|
+
const realDir = realpathSync(recipeDir);
|
|
926
|
+
const base = join(realDir, "package.json");
|
|
927
|
+
// Only the `node_modules` folders of the directory and its ancestors: not
|
|
928
|
+
// NODE_PATH or the global folders, which hold nothing the Recipe installed.
|
|
929
|
+
const lookupFolders = (createRequire(base).resolve.paths(name) ?? []).filter((folder) => {
|
|
930
|
+
if (basename(folder) !== "node_modules")
|
|
931
|
+
return false;
|
|
932
|
+
const owner = relative(dirname(folder), realDir);
|
|
933
|
+
return owner !== ".." && !owner.startsWith(`..${sep}`) && !isAbsolute(owner);
|
|
934
|
+
});
|
|
935
|
+
for (const folder of lookupFolders) {
|
|
936
|
+
const candidate = join(folder, ...name.split("/"));
|
|
937
|
+
if (existsSync(join(candidate, "package.json")))
|
|
938
|
+
return realpathSync(candidate);
|
|
939
|
+
}
|
|
940
|
+
try {
|
|
941
|
+
const found = findPackageJSON(name, base);
|
|
942
|
+
return found ? realpathSync(dirname(found)) : undefined;
|
|
943
|
+
}
|
|
944
|
+
catch {
|
|
945
|
+
return undefined;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* The Recipe's direct dependencies that are installed, in declaration order,
|
|
950
|
+
* from the `package.json` blocks named by `keys`, each found with Node's
|
|
951
|
+
* resolution from the Recipe directory. Transitive dependencies are never
|
|
952
|
+
* read, and a declared dependency that does not resolve is skipped.
|
|
953
|
+
*/
|
|
954
|
+
export function installedRecipeDependencies(recipeDir, keys) {
|
|
955
|
+
const root = resolve(recipeDir);
|
|
956
|
+
const packagePath = packageJsonPath(root);
|
|
957
|
+
if (!packagePath)
|
|
958
|
+
return [];
|
|
959
|
+
const raw = readJsonFile(packagePath);
|
|
960
|
+
const names = new Set();
|
|
961
|
+
for (const key of keys) {
|
|
962
|
+
for (const name of Object.keys(asRecord(raw[key])))
|
|
963
|
+
names.add(name);
|
|
964
|
+
}
|
|
965
|
+
const installed = [];
|
|
966
|
+
for (const name of names) {
|
|
967
|
+
const packageDir = resolveDependencyDir(root, name);
|
|
968
|
+
if (packageDir)
|
|
969
|
+
installed.push({ name, packageDir });
|
|
970
|
+
}
|
|
971
|
+
return installed;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* The skill resources each direct `dependencies` or `peerDependencies` entry
|
|
975
|
+
* declares with an explicit `pi.skills`, resolved inside that package (a peer
|
|
976
|
+
* is provided by the platform, often from an ancestor `node_modules`). One
|
|
977
|
+
* that is not installed is skipped. Dev and transitive dependencies are not
|
|
978
|
+
* read, a dependency without `pi.skills`
|
|
979
|
+
* contributes nothing (its `skills/` folder is not discovered by convention),
|
|
980
|
+
* and an installed dependency whose `pi.skills` is malformed, unmatched or
|
|
981
|
+
* escapes the package throws an error naming it.
|
|
982
|
+
*/
|
|
983
|
+
export function recipeDependencySkillResources(recipeDir) {
|
|
984
|
+
const resources = [];
|
|
985
|
+
for (const { name, packageDir } of installedRecipeDependencies(recipeDir, ["dependencies", "peerDependencies"])) {
|
|
986
|
+
const raw = readJsonFile(join(packageDir, "package.json"));
|
|
987
|
+
const pi = piPackageManifestBlock(raw);
|
|
988
|
+
if (!pi || pi.skills === undefined)
|
|
989
|
+
continue;
|
|
990
|
+
const owner = `Dependency ${name}`;
|
|
991
|
+
if (!Array.isArray(pi.skills) || pi.skills.some((item) => typeof item !== "string" || !item.trim())) {
|
|
992
|
+
throw new RecipePackageError(`${owner} package.json#pi.skills must be an array of non-empty strings`);
|
|
993
|
+
}
|
|
994
|
+
const pkg = {
|
|
995
|
+
name,
|
|
996
|
+
version: stringValue(raw.version) ?? "0.0.0",
|
|
997
|
+
path: packageDir,
|
|
998
|
+
resources: { ...emptyResources(), skills: stringArray(pi.skills).map(normalizeResourcePath) },
|
|
999
|
+
channels: [],
|
|
1000
|
+
documents: [],
|
|
1001
|
+
mcp: { manifests: [], servers: [] },
|
|
1002
|
+
};
|
|
1003
|
+
for (const path of resolvePiPackageResourcePaths(pkg, "skills", owner)) {
|
|
1004
|
+
resources.push({ path, packageDir, dependency: name });
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return resources;
|
|
1008
|
+
}
|
|
824
1009
|
//# sourceMappingURL=package.js.map
|