|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import { dirname, join, resolve as resolvePath } from "node:path"; |
| 4 | +import type { BuildManifest, SkillManifest } from "@trigger.dev/core/v3/schemas"; |
| 5 | +import { copyDirectoryRecursive } from "@trigger.dev/build/internal"; |
| 6 | +import { indexWorkerManifest } from "../indexing/indexWorkerManifest.js"; |
| 7 | +import { execOptionsForRuntime, type BuildLogger } from "@trigger.dev/core/v3/build"; |
| 8 | + |
| 9 | +export type BundleSkillsOptions = { |
| 10 | + buildManifest: BuildManifest; |
| 11 | + buildManifestPath: string; |
| 12 | + workingDir: string; |
| 13 | + env: Record<string, string | undefined>; |
| 14 | + logger: BuildLogger; |
| 15 | +}; |
| 16 | + |
| 17 | +export type BundleSkillsResult = { |
| 18 | + /** The input manifest, annotated with `skills` on return. */ |
| 19 | + buildManifest: BuildManifest; |
| 20 | + /** Discovered skills, in deterministic order. */ |
| 21 | + skills: SkillManifest[]; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Built-in skill bundler — not an extension. Runs the indexer locally |
| 26 | + * against the bundled worker output to discover `ai.defineSkill(...)` |
| 27 | + * registrations, validates each skill's `SKILL.md`, and copies the |
| 28 | + * folder into `{outputPath}/.trigger/skills/{id}/` so the deploy image |
| 29 | + * picks it up via the existing Dockerfile `COPY`. |
| 30 | + * |
| 31 | + * No `trigger.config.ts` changes required — discovery is side-effect |
| 32 | + * based, same mechanism as task/prompt registration. |
| 33 | + */ |
| 34 | +export async function bundleSkills( |
| 35 | + options: BundleSkillsOptions |
| 36 | +): Promise<BundleSkillsResult> { |
| 37 | + const { buildManifest, buildManifestPath, workingDir, env, logger } = options; |
| 38 | + |
| 39 | + let skills: SkillManifest[]; |
| 40 | + try { |
| 41 | + const workerManifest = await indexWorkerManifest({ |
| 42 | + runtime: buildManifest.runtime, |
| 43 | + indexWorkerPath: buildManifest.indexWorkerEntryPoint, |
| 44 | + buildManifestPath, |
| 45 | + nodeOptions: execOptionsForRuntime(buildManifest.runtime, buildManifest), |
| 46 | + env, |
| 47 | + cwd: workingDir, |
| 48 | + otelHookInclude: buildManifest.otelImportHook?.include, |
| 49 | + otelHookExclude: buildManifest.otelImportHook?.exclude, |
| 50 | + handleStdout(data) { |
| 51 | + logger.debug(`[bundleSkills] ${data}`); |
| 52 | + }, |
| 53 | + handleStderr(data) { |
| 54 | + if (!data.includes("Debugger attached")) { |
| 55 | + logger.debug(`[bundleSkills:stderr] ${data}`); |
| 56 | + } |
| 57 | + }, |
| 58 | + }); |
| 59 | + skills = workerManifest.skills ?? []; |
| 60 | + } catch (err) { |
| 61 | + // Skill discovery via the indexer is best-effort — if the user's |
| 62 | + // bundle doesn't load cleanly here the downstream full indexer will |
| 63 | + // surface the real error. Warn and continue with no skills. |
| 64 | + logger.debug(`[bundleSkills] skill discovery failed: ${(err as Error).message}`); |
| 65 | + return { buildManifest, skills: [] }; |
| 66 | + } |
| 67 | + |
| 68 | + if (skills.length === 0) { |
| 69 | + return { buildManifest, skills: [] }; |
| 70 | + } |
| 71 | + |
| 72 | + const destinationRoot = join(buildManifest.outputPath, ".trigger", "skills"); |
| 73 | + |
| 74 | + for (const skill of skills) { |
| 75 | + const sourcePath = resolvePath(workingDir, skill.sourcePath); |
| 76 | + const skillMdPath = join(sourcePath, "SKILL.md"); |
| 77 | + |
| 78 | + let skillMd: string; |
| 79 | + try { |
| 80 | + skillMd = await readFile(skillMdPath, "utf8"); |
| 81 | + } catch { |
| 82 | + throw new Error( |
| 83 | + `Skill "${skill.id}": SKILL.md not found at ${skillMdPath}. ` + |
| 84 | + `Registered via ai.defineSkill({ id: "${skill.id}", path: "${skill.sourcePath}" }) ` + |
| 85 | + `at ${skill.filePath}.` |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + if (!/^---\r?\n[\s\S]*?\r?\n---/.test(skillMd)) { |
| 90 | + throw new Error( |
| 91 | + `Skill "${skill.id}": SKILL.md at ${skillMdPath} is missing a frontmatter block.` |
| 92 | + ); |
| 93 | + } |
| 94 | + if (!/\bname:\s*\S/.test(skillMd) || !/\bdescription:\s*\S/.test(skillMd)) { |
| 95 | + throw new Error( |
| 96 | + `Skill "${skill.id}": SKILL.md at ${skillMdPath} frontmatter must include both \`name\` and \`description\`.` |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + const skillDest = join(destinationRoot, skill.id); |
| 101 | + logger.debug(`[bundleSkills] Copying ${sourcePath} → ${skillDest}`); |
| 102 | + await copyDirectoryRecursive(sourcePath, skillDest); |
| 103 | + } |
| 104 | + |
| 105 | + // Sort by id for deterministic manifest output |
| 106 | + skills = [...skills].sort((a, b) => a.id.localeCompare(b.id)); |
| 107 | + |
| 108 | + // Content hash is derived from each SKILL.md's content for cache invalidation |
| 109 | + // downstream (dashboard persistence in Phase 2). Not used in Phase 1. |
| 110 | + void createHash; |
| 111 | + void dirname; |
| 112 | + |
| 113 | + return { |
| 114 | + buildManifest: { ...buildManifest, skills }, |
| 115 | + skills, |
| 116 | + }; |
| 117 | +} |
0 commit comments