{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../src/pack/export.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAGN,KAAK,eAAe,EAMpB,MAAM,WAAW,CAAC;AAgCnB,MAAM,WAAW,oBAAoB;IACpC,KAAK,EAAE,QAAQ,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,eAAe,CAAC,CA0G3F","sourcesContent":["import { mkdir, readFile, rm, writeFile } from \"node:fs/promises\";\nimport { basename, dirname, join } from \"node:path\";\nimport { loadCompiledBundle } from \"../bundle/compile.ts\";\nimport { type LoadedEvoComponentArtifact, validateEvoComponentSelection } from \"../components/artifact.ts\";\nimport { createDefaultEvoAbiRegistry, type EvoAbiRegistry } from \"../components/registry.ts\";\nimport { PREFERENCES_PATH } from \"../memory/preferences.ts\";\nimport type { EvoPaths } from \"../paths.ts\";\nimport type { EvoComponentSelection } from \"../types.ts\";\nimport {\n\tcomputeEvoPackIntegrity,\n\ttype EvoPackComponent,\n\ttype EvoPackManifest,\n\ttype EvoPackMemory,\n\ttype EvoPackPrompt,\n\ttype EvoPackSkill,\n\ttype EvoPackWorkflow,\n\tparseEvoPackManifest,\n} from \"./pack.ts\";\n\nasync function copyFile(source: string, destination: string): Promise<void> {\n\tawait mkdir(dirname(destination), { recursive: true, mode: 0o700 });\n\tawait writeFile(destination, await readFile(source), { mode: 0o600, flag: \"wx\" });\n}\n\nasync function copySelectedArtifact(options: {\n\tpaths: EvoPaths;\n\tregistry: EvoAbiRegistry;\n\tsurface: string;\n\tselection: EvoComponentSelection;\n\ttargetDirectory: string;\n\trelativeDirectory: string;\n}): Promise<LoadedEvoComponentArtifact> {\n\tconst artifact = await validateEvoComponentSelection(\n\t\toptions.paths,\n\t\toptions.surface,\n\t\toptions.selection,\n\t\toptions.registry,\n\t);\n\tawait copyFile(\n\t\tjoin(artifact.directory, \"manifest.json\"),\n\t\tjoin(options.targetDirectory, options.relativeDirectory, \"manifest.json\"),\n\t);\n\tawait copyFile(\n\t\tartifact.entrypoint,\n\t\tjoin(options.targetDirectory, options.relativeDirectory, artifact.manifest.entrypoint),\n\t);\n\treturn artifact;\n}\n\nexport interface ExportEvoPackOptions {\n\tpaths: EvoPaths;\n\tbundleDigest: string;\n\ttargetDirectory: string;\n\tname: string;\n\tversion: string;\n\tauthor?: string;\n\tdescription?: string;\n}\n\n/**\n * Export bundle-owned data and selected registered components as pack.json v1.\n * The destination must not already exist; failures remove the incomplete tree.\n */\nexport async function exportEvoPack(options: ExportEvoPackOptions): Promise<EvoPackManifest> {\n\tconst bundle = await loadCompiledBundle(options.paths, options.bundleDigest);\n\tawait mkdir(options.targetDirectory, { mode: 0o700 });\n\ttry {\n\t\tconst prompts: EvoPackPrompt[] = [];\n\t\tconst skills: EvoPackSkill[] = [];\n\t\tconst memory: EvoPackMemory[] = [];\n\t\tconst components: EvoPackComponent[] = [];\n\t\tconst workflows: EvoPackWorkflow[] = [];\n\t\tfor (const file of bundle.manifest.files) {\n\t\t\tif (file.path.startsWith(\"prompts/\")) {\n\t\t\t\tawait copyFile(join(bundle.directory, file.path), join(options.targetDirectory, file.path));\n\t\t\t\tprompts.push({\n\t\t\t\t\ttarget: file.path === \"prompts/system.md\" ? \"system\" : \"append-system\",\n\t\t\t\t\tfile: file.path,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (file.path.startsWith(\"skills/\")) {\n\t\t\t\tawait copyFile(join(bundle.directory, file.path), join(options.targetDirectory, file.path));\n\t\t\t\tskills.push({ name: file.path.split(\"/\")[1], dir: dirname(file.path) });\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (file.path === PREFERENCES_PATH) {\n\t\t\t\tconst destination = `memory/${basename(file.path)}`;\n\t\t\t\tawait copyFile(join(bundle.directory, file.path), join(options.targetDirectory, destination));\n\t\t\t\tmemory.push({ file: destination });\n\t\t\t}\n\t\t}\n\t\tconst registry = createDefaultEvoAbiRegistry();\n\t\tfor (const [surface, selection] of Object.entries(bundle.policy.components ?? {})) {\n\t\t\tconst relativeDirectory = `components/${selection.id}`;\n\t\t\tconst artifact = await copySelectedArtifact({\n\t\t\t\tpaths: options.paths,\n\t\t\t\tregistry,\n\t\t\t\tsurface,\n\t\t\t\tselection,\n\t\t\t\ttargetDirectory: options.targetDirectory,\n\t\t\t\trelativeDirectory,\n\t\t\t});\n\t\t\tcomponents.push({\n\t\t\t\tsurface,\n\t\t\t\tabi: artifact.manifest.abi,\n\t\t\t\tid: artifact.manifest.id,\n\t\t\t\tartifact: relativeDirectory,\n\t\t\t\tcapabilities: artifact.manifest.capabilities,\n\t\t\t});\n\t\t}\n\t\tfor (const selection of bundle.policy.tools ?? []) {\n\t\t\tconst relativeDirectory = `components/${selection.id}`;\n\t\t\tconst artifact = await copySelectedArtifact({\n\t\t\t\tpaths: options.paths,\n\t\t\t\tregistry,\n\t\t\t\tsurface: \"tool\",\n\t\t\t\tselection,\n\t\t\t\ttargetDirectory: options.targetDirectory,\n\t\t\t\trelativeDirectory,\n\t\t\t});\n\t\t\tcomponents.push({\n\t\t\t\tsurface: \"tool\",\n\t\t\t\tabi: artifact.manifest.abi,\n\t\t\t\tid: artifact.manifest.id,\n\t\t\t\tartifact: relativeDirectory,\n\t\t\t\tcapabilities: artifact.manifest.capabilities,\n\t\t\t});\n\t\t}\n\t\tfor (const selection of bundle.policy.workflows ?? []) {\n\t\t\tconst relativeDirectory = `workflows/${selection.id}`;\n\t\t\tconst artifact = await copySelectedArtifact({\n\t\t\t\tpaths: options.paths,\n\t\t\t\tregistry,\n\t\t\t\tsurface: \"workflow\",\n\t\t\t\tselection,\n\t\t\t\ttargetDirectory: options.targetDirectory,\n\t\t\t\trelativeDirectory,\n\t\t\t});\n\t\t\tworkflows.push({\n\t\t\t\tid: artifact.manifest.id,\n\t\t\t\ttrigger: selection.trigger,\n\t\t\t\tabi: artifact.manifest.abi,\n\t\t\t\tartifact: relativeDirectory,\n\t\t\t\tcapabilities: artifact.manifest.capabilities,\n\t\t\t});\n\t\t}\n\t\tconst codeParts = [...components, ...workflows];\n\t\tconst unsigned = parseEvoPackManifest({\n\t\t\tpackFormat: 1,\n\t\t\tname: options.name,\n\t\t\tversion: options.version,\n\t\t\t...(options.author === undefined ? {} : { author: options.author }),\n\t\t\t...(options.description === undefined ? {} : { description: options.description }),\n\t\t\tcontents: { prompts, skills, memory, components, workflows },\n\t\t\trequiresAbis: [...new Set(codeParts.map((component) => component.abi))],\n\t\t\trequiresCapabilities: [...new Set(codeParts.flatMap((component) => component.capabilities))],\n\t\t});\n\t\tconst integrity = await computeEvoPackIntegrity(options.targetDirectory, unsigned);\n\t\tconst manifest = parseEvoPackManifest({ ...unsigned, integrity });\n\t\tawait writeFile(join(options.targetDirectory, \"pack.json\"), `${JSON.stringify(manifest, undefined, \"\\t\")}\\n`, {\n\t\t\tmode: 0o600,\n\t\t\tflag: \"wx\",\n\t\t});\n\t\treturn manifest;\n\t} catch (error) {\n\t\tawait rm(options.targetDirectory, { recursive: true, force: true });\n\t\tthrow error;\n\t}\n}\n"]}