@prompd/cli 0.3.3

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.
Files changed (223) hide show
  1. package/README.md +162 -0
  2. package/bin/prompd.js +23 -0
  3. package/dist/commands/cache.d.ts +3 -0
  4. package/dist/commands/cache.d.ts.map +1 -0
  5. package/dist/commands/cache.js +199 -0
  6. package/dist/commands/cache.js.map +1 -0
  7. package/dist/commands/compile.d.ts +9 -0
  8. package/dist/commands/compile.d.ts.map +1 -0
  9. package/dist/commands/compile.js +104 -0
  10. package/dist/commands/compile.js.map +1 -0
  11. package/dist/commands/config.d.ts +7 -0
  12. package/dist/commands/config.d.ts.map +1 -0
  13. package/dist/commands/config.js +212 -0
  14. package/dist/commands/config.js.map +1 -0
  15. package/dist/commands/create.d.ts +3 -0
  16. package/dist/commands/create.d.ts.map +1 -0
  17. package/dist/commands/create.js +183 -0
  18. package/dist/commands/create.js.map +1 -0
  19. package/dist/commands/deps.d.ts +3 -0
  20. package/dist/commands/deps.d.ts.map +1 -0
  21. package/dist/commands/deps.js +192 -0
  22. package/dist/commands/deps.js.map +1 -0
  23. package/dist/commands/explain.d.ts +3 -0
  24. package/dist/commands/explain.d.ts.map +1 -0
  25. package/dist/commands/explain.js +227 -0
  26. package/dist/commands/explain.js.map +1 -0
  27. package/dist/commands/git.d.ts +3 -0
  28. package/dist/commands/git.d.ts.map +1 -0
  29. package/dist/commands/git.js +306 -0
  30. package/dist/commands/git.js.map +1 -0
  31. package/dist/commands/init.d.ts +3 -0
  32. package/dist/commands/init.d.ts.map +1 -0
  33. package/dist/commands/init.js +177 -0
  34. package/dist/commands/init.js.map +1 -0
  35. package/dist/commands/list.d.ts +3 -0
  36. package/dist/commands/list.d.ts.map +1 -0
  37. package/dist/commands/list.js +126 -0
  38. package/dist/commands/list.js.map +1 -0
  39. package/dist/commands/mcp.d.ts +3 -0
  40. package/dist/commands/mcp.d.ts.map +1 -0
  41. package/dist/commands/mcp.js +326 -0
  42. package/dist/commands/mcp.js.map +1 -0
  43. package/dist/commands/namespace.d.ts +3 -0
  44. package/dist/commands/namespace.d.ts.map +1 -0
  45. package/dist/commands/namespace.js +113 -0
  46. package/dist/commands/namespace.js.map +1 -0
  47. package/dist/commands/package.d.ts +23 -0
  48. package/dist/commands/package.d.ts.map +1 -0
  49. package/dist/commands/package.js +746 -0
  50. package/dist/commands/package.js.map +1 -0
  51. package/dist/commands/provider.d.ts +3 -0
  52. package/dist/commands/provider.d.ts.map +1 -0
  53. package/dist/commands/provider.js +285 -0
  54. package/dist/commands/provider.js.map +1 -0
  55. package/dist/commands/registry.d.ts +9 -0
  56. package/dist/commands/registry.d.ts.map +1 -0
  57. package/dist/commands/registry.js +361 -0
  58. package/dist/commands/registry.js.map +1 -0
  59. package/dist/commands/run.d.ts +3 -0
  60. package/dist/commands/run.d.ts.map +1 -0
  61. package/dist/commands/run.js +157 -0
  62. package/dist/commands/run.js.map +1 -0
  63. package/dist/commands/show.d.ts +3 -0
  64. package/dist/commands/show.d.ts.map +1 -0
  65. package/dist/commands/show.js +90 -0
  66. package/dist/commands/show.js.map +1 -0
  67. package/dist/commands/uninstall.d.ts +3 -0
  68. package/dist/commands/uninstall.d.ts.map +1 -0
  69. package/dist/commands/uninstall.js +95 -0
  70. package/dist/commands/uninstall.js.map +1 -0
  71. package/dist/commands/validate.d.ts +3 -0
  72. package/dist/commands/validate.d.ts.map +1 -0
  73. package/dist/commands/validate.js +57 -0
  74. package/dist/commands/validate.js.map +1 -0
  75. package/dist/commands/version.d.ts +3 -0
  76. package/dist/commands/version.d.ts.map +1 -0
  77. package/dist/commands/version.js +166 -0
  78. package/dist/commands/version.js.map +1 -0
  79. package/dist/index.d.ts +5 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +388 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/lib/auth.d.ts +164 -0
  84. package/dist/lib/auth.d.ts.map +1 -0
  85. package/dist/lib/auth.js +388 -0
  86. package/dist/lib/auth.js.map +1 -0
  87. package/dist/lib/compiler/file-system.d.ts +178 -0
  88. package/dist/lib/compiler/file-system.d.ts.map +1 -0
  89. package/dist/lib/compiler/file-system.js +440 -0
  90. package/dist/lib/compiler/file-system.js.map +1 -0
  91. package/dist/lib/compiler/formatters/anthropic.d.ts +21 -0
  92. package/dist/lib/compiler/formatters/anthropic.d.ts.map +1 -0
  93. package/dist/lib/compiler/formatters/anthropic.js +95 -0
  94. package/dist/lib/compiler/formatters/anthropic.js.map +1 -0
  95. package/dist/lib/compiler/formatters/markdown.d.ts +17 -0
  96. package/dist/lib/compiler/formatters/markdown.d.ts.map +1 -0
  97. package/dist/lib/compiler/formatters/markdown.js +114 -0
  98. package/dist/lib/compiler/formatters/markdown.js.map +1 -0
  99. package/dist/lib/compiler/formatters/openai.d.ts +21 -0
  100. package/dist/lib/compiler/formatters/openai.d.ts.map +1 -0
  101. package/dist/lib/compiler/formatters/openai.js +98 -0
  102. package/dist/lib/compiler/formatters/openai.js.map +1 -0
  103. package/dist/lib/compiler/index.d.ts +56 -0
  104. package/dist/lib/compiler/index.d.ts.map +1 -0
  105. package/dist/lib/compiler/index.js +165 -0
  106. package/dist/lib/compiler/index.js.map +1 -0
  107. package/dist/lib/compiler/language-map.d.ts +31 -0
  108. package/dist/lib/compiler/language-map.d.ts.map +1 -0
  109. package/dist/lib/compiler/language-map.js +156 -0
  110. package/dist/lib/compiler/language-map.js.map +1 -0
  111. package/dist/lib/compiler/package-resolver.d.ts +68 -0
  112. package/dist/lib/compiler/package-resolver.d.ts.map +1 -0
  113. package/dist/lib/compiler/package-resolver.js +254 -0
  114. package/dist/lib/compiler/package-resolver.js.map +1 -0
  115. package/dist/lib/compiler/pipeline.d.ts +53 -0
  116. package/dist/lib/compiler/pipeline.d.ts.map +1 -0
  117. package/dist/lib/compiler/pipeline.js +209 -0
  118. package/dist/lib/compiler/pipeline.js.map +1 -0
  119. package/dist/lib/compiler/prompd-loader.d.ts +108 -0
  120. package/dist/lib/compiler/prompd-loader.d.ts.map +1 -0
  121. package/dist/lib/compiler/prompd-loader.js +270 -0
  122. package/dist/lib/compiler/prompd-loader.js.map +1 -0
  123. package/dist/lib/compiler/section-override.d.ts +40 -0
  124. package/dist/lib/compiler/section-override.d.ts.map +1 -0
  125. package/dist/lib/compiler/section-override.js +296 -0
  126. package/dist/lib/compiler/section-override.js.map +1 -0
  127. package/dist/lib/compiler/stages/assets.d.ts +71 -0
  128. package/dist/lib/compiler/stages/assets.d.ts.map +1 -0
  129. package/dist/lib/compiler/stages/assets.js +456 -0
  130. package/dist/lib/compiler/stages/assets.js.map +1 -0
  131. package/dist/lib/compiler/stages/codegen.d.ts +17 -0
  132. package/dist/lib/compiler/stages/codegen.d.ts.map +1 -0
  133. package/dist/lib/compiler/stages/codegen.js +64 -0
  134. package/dist/lib/compiler/stages/codegen.js.map +1 -0
  135. package/dist/lib/compiler/stages/dependency.d.ts +38 -0
  136. package/dist/lib/compiler/stages/dependency.d.ts.map +1 -0
  137. package/dist/lib/compiler/stages/dependency.js +307 -0
  138. package/dist/lib/compiler/stages/dependency.js.map +1 -0
  139. package/dist/lib/compiler/stages/lexical.d.ts +19 -0
  140. package/dist/lib/compiler/stages/lexical.d.ts.map +1 -0
  141. package/dist/lib/compiler/stages/lexical.js +92 -0
  142. package/dist/lib/compiler/stages/lexical.js.map +1 -0
  143. package/dist/lib/compiler/stages/semantic.d.ts +20 -0
  144. package/dist/lib/compiler/stages/semantic.d.ts.map +1 -0
  145. package/dist/lib/compiler/stages/semantic.js +166 -0
  146. package/dist/lib/compiler/stages/semantic.js.map +1 -0
  147. package/dist/lib/compiler/stages/template.d.ts +94 -0
  148. package/dist/lib/compiler/stages/template.d.ts.map +1 -0
  149. package/dist/lib/compiler/stages/template.js +1044 -0
  150. package/dist/lib/compiler/stages/template.js.map +1 -0
  151. package/dist/lib/compiler/types.d.ts +200 -0
  152. package/dist/lib/compiler/types.d.ts.map +1 -0
  153. package/dist/lib/compiler/types.js +137 -0
  154. package/dist/lib/compiler/types.js.map +1 -0
  155. package/dist/lib/config.d.ts +29 -0
  156. package/dist/lib/config.d.ts.map +1 -0
  157. package/dist/lib/config.js +375 -0
  158. package/dist/lib/config.js.map +1 -0
  159. package/dist/lib/errors.d.ts +19 -0
  160. package/dist/lib/errors.d.ts.map +1 -0
  161. package/dist/lib/errors.js +47 -0
  162. package/dist/lib/errors.js.map +1 -0
  163. package/dist/lib/executor.d.ts +18 -0
  164. package/dist/lib/executor.d.ts.map +1 -0
  165. package/dist/lib/executor.js +372 -0
  166. package/dist/lib/executor.js.map +1 -0
  167. package/dist/lib/git.d.ts +74 -0
  168. package/dist/lib/git.d.ts.map +1 -0
  169. package/dist/lib/git.js +254 -0
  170. package/dist/lib/git.js.map +1 -0
  171. package/dist/lib/index.d.ts +43 -0
  172. package/dist/lib/index.d.ts.map +1 -0
  173. package/dist/lib/index.js +108 -0
  174. package/dist/lib/index.js.map +1 -0
  175. package/dist/lib/mcp.d.ts +42 -0
  176. package/dist/lib/mcp.d.ts.map +1 -0
  177. package/dist/lib/mcp.js +477 -0
  178. package/dist/lib/mcp.js.map +1 -0
  179. package/dist/lib/model-updater.d.ts +51 -0
  180. package/dist/lib/model-updater.d.ts.map +1 -0
  181. package/dist/lib/model-updater.js +275 -0
  182. package/dist/lib/model-updater.js.map +1 -0
  183. package/dist/lib/parser.d.ts +9 -0
  184. package/dist/lib/parser.d.ts.map +1 -0
  185. package/dist/lib/parser.js +197 -0
  186. package/dist/lib/parser.js.map +1 -0
  187. package/dist/lib/registry.d.ts +183 -0
  188. package/dist/lib/registry.d.ts.map +1 -0
  189. package/dist/lib/registry.js +786 -0
  190. package/dist/lib/registry.js.map +1 -0
  191. package/dist/lib/rpc-server.d.ts +78 -0
  192. package/dist/lib/rpc-server.d.ts.map +1 -0
  193. package/dist/lib/rpc-server.js +404 -0
  194. package/dist/lib/rpc-server.js.map +1 -0
  195. package/dist/lib/security.d.ts +120 -0
  196. package/dist/lib/security.d.ts.map +1 -0
  197. package/dist/lib/security.js +478 -0
  198. package/dist/lib/security.js.map +1 -0
  199. package/dist/lib/validation.d.ts +106 -0
  200. package/dist/lib/validation.d.ts.map +1 -0
  201. package/dist/lib/validation.js +398 -0
  202. package/dist/lib/validation.js.map +1 -0
  203. package/dist/lib/version.d.ts +29 -0
  204. package/dist/lib/version.d.ts.map +1 -0
  205. package/dist/lib/version.js +202 -0
  206. package/dist/lib/version.js.map +1 -0
  207. package/dist/lib/workflow-engine.d.ts +161 -0
  208. package/dist/lib/workflow-engine.d.ts.map +1 -0
  209. package/dist/lib/workflow-engine.js +422 -0
  210. package/dist/lib/workflow-engine.js.map +1 -0
  211. package/dist/lib/workflow.d.ts +102 -0
  212. package/dist/lib/workflow.d.ts.map +1 -0
  213. package/dist/lib/workflow.js +228 -0
  214. package/dist/lib/workflow.js.map +1 -0
  215. package/dist/server.d.ts +8 -0
  216. package/dist/server.d.ts.map +1 -0
  217. package/dist/server.js +134 -0
  218. package/dist/server.js.map +1 -0
  219. package/dist/types/index.d.ts +116 -0
  220. package/dist/types/index.d.ts.map +1 -0
  221. package/dist/types/index.js +144 -0
  222. package/dist/types/index.js.map +1 -0
  223. package/package.json +104 -0
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ /**
3
+ * Compilation pipeline orchestrator.
4
+ *
5
+ * Manages the execution of the 6-stage compilation pipeline, ensuring stages
6
+ * run in order and errors are properly propagated.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.CompilerPipeline = void 0;
43
+ const path = __importStar(require("path"));
44
+ const fs = __importStar(require("fs-extra"));
45
+ const types_1 = require("./types");
46
+ const errors_1 = require("../errors");
47
+ const file_system_1 = require("./file-system");
48
+ /**
49
+ * The main compiler pipeline orchestrator.
50
+ */
51
+ class CompilerPipeline {
52
+ constructor(stages, securityConfig) {
53
+ this.stages = stages || [];
54
+ this.securityConfig = securityConfig || types_1.DEFAULT_SECURITY_CONFIG;
55
+ }
56
+ /**
57
+ * Register a compilation stage.
58
+ */
59
+ registerStage(stage) {
60
+ this.stages.push(stage);
61
+ }
62
+ /**
63
+ * Execute the compilation pipeline.
64
+ *
65
+ * @param source - Path to .prmd file or package reference
66
+ * @param options - Compilation options
67
+ * @returns Compilation context with result or errors
68
+ */
69
+ async execute(source, options = {}) {
70
+ // Set file system early (use provided or default to Node.js fs)
71
+ const fileSystem = options.fileSystem || (0, file_system_1.getDefaultFileSystem)();
72
+ // Resolve source (handles both file paths and package references)
73
+ const sourcePath = await this.resolveSource(source, fileSystem, options.fileSystem, options.registryUrl, options.workspaceRoot);
74
+ // Security: Validate file exists and is within allowed paths (skip if using custom FS)
75
+ if (!options.fileSystem) {
76
+ await this.validateSourceFile(sourcePath);
77
+ }
78
+ // Create compilation context
79
+ const context = new types_1.CompilationContext(sourcePath, options);
80
+ // Set file system
81
+ context.fileSystem = fileSystem;
82
+ // Run each stage in sequence
83
+ // Continue through all stages to accumulate all errors (don't stop at first error)
84
+ for (const stage of this.stages) {
85
+ try {
86
+ if (context.verbose) {
87
+ console.log(`Running stage: ${stage.getName()}`);
88
+ }
89
+ await stage.process(context);
90
+ }
91
+ catch (error) {
92
+ const errorMessage = error instanceof Error ? error.message : String(error);
93
+ context.addError(`${stage.getName()} failed: ${errorMessage}`);
94
+ // Continue to next stage to find additional errors
95
+ }
96
+ }
97
+ return context;
98
+ }
99
+ /**
100
+ * Resolve source to file path (handles package references).
101
+ *
102
+ * @param source - Package reference or file path
103
+ * @param fileSystem - File system to use for resolution
104
+ * @param customFileSystem - Optional custom file system (used to detect in-memory mode)
105
+ * @param registryUrl - Optional registry URL for package resolution
106
+ * @param workspaceRoot - Optional workspace root for package cache location
107
+ */
108
+ async resolveSource(source, fileSystem, customFileSystem, registryUrl, workspaceRoot) {
109
+ // Check if it's a package reference (starts with @)
110
+ if (source.startsWith('@')) {
111
+ // Import package resolver
112
+ const { resolvePackage } = await Promise.resolve().then(() => __importStar(require('./package-resolver')));
113
+ try {
114
+ // Pass options to resolvePackage for proper package resolution
115
+ const packagePath = await resolvePackage(source, {
116
+ fileSystem,
117
+ registryUrl,
118
+ workspaceRoot
119
+ });
120
+ // Find the main .prmd file in the package
121
+ const prmdFiles = await this.findPromdFiles(packagePath, fileSystem);
122
+ if (prmdFiles.length === 0) {
123
+ throw new Error(`No .prmd files found in package: ${source}`);
124
+ }
125
+ // Use the first .prmd file (or could look for main.prmd)
126
+ return prmdFiles[0];
127
+ }
128
+ catch (error) {
129
+ throw new errors_1.PrompdError(`Failed to resolve package ${source}: ${error}`);
130
+ }
131
+ }
132
+ // Regular file path
133
+ if (customFileSystem) {
134
+ // Custom file system (e.g., MemoryFileSystem) - don't resolve to absolute OS path
135
+ // Virtual paths like '/main.prmd' should stay as-is
136
+ return source;
137
+ }
138
+ // Disk-based file system - resolve to absolute path
139
+ return path.resolve(source);
140
+ }
141
+ /**
142
+ * Find all .prmd files in a directory.
143
+ *
144
+ * @param dir - Directory path
145
+ * @param fileSystem - File system to use
146
+ */
147
+ async findPromdFiles(dir, fileSystem) {
148
+ const files = [];
149
+ try {
150
+ // Use fileSystem abstraction instead of fs module
151
+ const entries = await fileSystem.readdir(dir);
152
+ for (const entry of entries) {
153
+ const fullPath = fileSystem.join(dir, entry);
154
+ if (await fileSystem.isDirectory(fullPath)) {
155
+ // Recursively search subdirectories
156
+ const subFiles = await this.findPromdFiles(fullPath, fileSystem);
157
+ files.push(...subFiles);
158
+ }
159
+ else if (entry.endsWith('.prmd')) {
160
+ files.push(fullPath);
161
+ }
162
+ }
163
+ }
164
+ catch (error) {
165
+ // Ignore errors (directory not accessible, etc.)
166
+ }
167
+ return files;
168
+ }
169
+ /**
170
+ * Validate source file for security.
171
+ */
172
+ async validateSourceFile(sourcePath) {
173
+ // Security: Check file exists
174
+ if (!await fs.pathExists(sourcePath)) {
175
+ throw new errors_1.PrompdError(`Source file not found: ${sourcePath}`);
176
+ }
177
+ // Security: Check file extension
178
+ const ext = path.extname(sourcePath).toLowerCase();
179
+ if (!this.securityConfig.allowedExtensions.includes(ext)) {
180
+ throw new errors_1.PrompdError(`File extension not allowed: ${ext}`);
181
+ }
182
+ // Security: Check file size
183
+ const stats = await fs.stat(sourcePath);
184
+ if (stats.size > this.securityConfig.maxFileSize) {
185
+ throw new errors_1.PrompdError(`File too large: ${stats.size} bytes (max: ${this.securityConfig.maxFileSize})`);
186
+ }
187
+ // Security: Ensure it's a regular file (not symlink, device, etc.)
188
+ if (!stats.isFile()) {
189
+ throw new errors_1.PrompdError(`Source must be a regular file: ${sourcePath}`);
190
+ }
191
+ // Security: Path traversal protection - ensure resolved path is valid
192
+ const resolvedPath = path.resolve(sourcePath);
193
+ if (resolvedPath !== sourcePath && !sourcePath.startsWith('.')) {
194
+ // Allow relative paths but validate they don't escape via ..
195
+ const normalized = path.normalize(sourcePath);
196
+ if (normalized.includes('..')) {
197
+ throw new errors_1.PrompdError(`Path traversal detected: ${sourcePath}`);
198
+ }
199
+ }
200
+ }
201
+ /**
202
+ * Get registered stages.
203
+ */
204
+ getStages() {
205
+ return [...this.stages];
206
+ }
207
+ }
208
+ exports.CompilerPipeline = CompilerPipeline;
209
+ //# sourceMappingURL=pipeline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pipeline.js","sourceRoot":"","sources":["../../../src/lib/compiler/pipeline.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAA6B;AAC7B,6CAA+B;AAC/B,mCAMiB;AACjB,sCAAwC;AACxC,+CAAkE;AAElE;;GAEG;AACH,MAAa,gBAAgB;IAI3B,YAAY,MAAwB,EAAE,cAA+B;QACnE,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAC3B,IAAI,CAAC,cAAc,GAAG,cAAc,IAAI,+BAAuB,CAAC;IAClE,CAAC;IAED;;OAEG;IACH,aAAa,CAAC,KAAoB;QAChC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,CAAC,MAAc,EAAE,UAA8B,EAAE;QAC5D,gEAAgE;QAChE,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,IAAA,kCAAoB,GAAE,CAAC;QAEhE,kEAAkE;QAClE,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;QAEhI,uFAAuF;QACvF,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;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CAAC,CAAC;QAC9C,IAAI,YAAY,KAAK,UAAU,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/D,6DAA6D;YAC7D,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;YAC9C,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC9B,MAAM,IAAI,oBAAW,CAAC,4BAA4B,UAAU,EAAE,CAAC,CAAC;YAClE,CAAC;QACH,CAAC;IACH,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;CACF;AAvLD,4CAuLC"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Custom Nunjucks Loader for Prompd Files
3
+ *
4
+ * This loader enables {% include %} directives in .prmd files with compile-aware behavior:
5
+ * - .prmd files: Parses and returns only the body content (frontmatter stripped)
6
+ * - Other files (.md, .txt, etc.): Returns raw content as-is
7
+ *
8
+ * Features:
9
+ * - Workspace-aware path resolution (relative to source file)
10
+ * - Circular include detection
11
+ * - Compile-on-demand for .prmd files
12
+ */
13
+ import * as nunjucks from 'nunjucks';
14
+ import { IFileSystem } from './file-system';
15
+ /**
16
+ * Options for the Prompd loader.
17
+ */
18
+ export interface PrompdLoaderOptions {
19
+ /** File system abstraction (real FS or in-memory) */
20
+ fileSystem: IFileSystem;
21
+ /** Base directory for resolving relative paths */
22
+ baseDir: string;
23
+ /** Enable verbose logging */
24
+ verbose?: boolean;
25
+ /** Maximum include depth to prevent infinite recursion */
26
+ maxDepth?: number;
27
+ /** Unique compilation ID for tracking include stack across loader instances */
28
+ compilationId?: string;
29
+ }
30
+ /**
31
+ * Loader result with source content and metadata.
32
+ * Matches Nunjucks LoaderSource interface.
33
+ */
34
+ interface LoaderResult {
35
+ src: string;
36
+ path: string;
37
+ noCache: boolean;
38
+ }
39
+ /**
40
+ * Custom Nunjucks loader that handles .prmd files specially.
41
+ *
42
+ * When a .prmd file is included, this loader:
43
+ * 1. Parses the frontmatter
44
+ * 2. Extracts only the body content
45
+ * 3. Returns the body for inclusion
46
+ *
47
+ * This allows prompts to be composed from other prompts without
48
+ * duplicating frontmatter or metadata.
49
+ */
50
+ export declare class PrompdLoader extends nunjucks.Loader {
51
+ async: boolean;
52
+ private fileSystem;
53
+ private baseDir;
54
+ private verbose;
55
+ private maxDepth;
56
+ private parser;
57
+ private compilationId;
58
+ constructor(options: PrompdLoaderOptions);
59
+ /**
60
+ * Get the include stack for this compilation.
61
+ */
62
+ private getIncludeStack;
63
+ /**
64
+ * Clean up the global stack after compilation is complete.
65
+ * Should be called when the top-level compilation finishes.
66
+ */
67
+ static cleanupCompilation(compilationId: string): void;
68
+ /**
69
+ * Get the source content for a template.
70
+ * This is the main entry point called by Nunjucks.
71
+ * Throws an error if the file is not found (required by Nunjucks interface).
72
+ */
73
+ getSource(name: string): LoaderResult;
74
+ /**
75
+ * Load and process a file.
76
+ * Note: We do NOT remove from stack after loading because Nunjucks will process
77
+ * the returned content which may contain more includes. The stack tracks the
78
+ * entire include chain during a single compilation.
79
+ */
80
+ private loadFile;
81
+ /**
82
+ * Process a .prmd file: parse frontmatter and return only the body.
83
+ */
84
+ private processPrompdFile;
85
+ /**
86
+ * Resolve a path relative to the base directory.
87
+ */
88
+ private resolvePath;
89
+ /**
90
+ * Create a child loader for nested includes.
91
+ * The child loader shares the compilation ID to use the same include stack.
92
+ */
93
+ createChildLoader(newBaseDir: string): PrompdLoader;
94
+ /**
95
+ * Get the compilation ID for this loader.
96
+ * Useful for cleanup after compilation.
97
+ */
98
+ getCompilationId(): string;
99
+ }
100
+ /**
101
+ * Create a Nunjucks environment configured with the Prompd loader.
102
+ *
103
+ * @param options - Loader options
104
+ * @returns Configured Nunjucks environment
105
+ */
106
+ export declare function createPrompdEnvironment(options: PrompdLoaderOptions): nunjucks.Environment;
107
+ export {};
108
+ //# sourceMappingURL=prompd-loader.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AC,WAAW;IAUnB;;;OAGG;IACH,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY;IAUnD;;;OAGG;IACH,gBAAgB,IAAI,MAAM;CAG3B;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,mBAAmB,GAAG,QAAQ,CAAC,WAAW,CAmB1F"}
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ /**
3
+ * Custom Nunjucks Loader for Prompd Files
4
+ *
5
+ * This loader enables {% include %} directives in .prmd files with compile-aware behavior:
6
+ * - .prmd files: Parses and returns only the body content (frontmatter stripped)
7
+ * - Other files (.md, .txt, etc.): Returns raw content as-is
8
+ *
9
+ * Features:
10
+ * - Workspace-aware path resolution (relative to source file)
11
+ * - Circular include detection
12
+ * - Compile-on-demand for .prmd files
13
+ */
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
26
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
27
+ }) : function(o, v) {
28
+ o["default"] = v;
29
+ });
30
+ var __importStar = (this && this.__importStar) || (function () {
31
+ var ownKeys = function(o) {
32
+ ownKeys = Object.getOwnPropertyNames || function (o) {
33
+ var ar = [];
34
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
35
+ return ar;
36
+ };
37
+ return ownKeys(o);
38
+ };
39
+ return function (mod) {
40
+ if (mod && mod.__esModule) return mod;
41
+ var result = {};
42
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ })();
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.PrompdLoader = void 0;
49
+ exports.createPrompdEnvironment = createPrompdEnvironment;
50
+ const nunjucks = __importStar(require("nunjucks"));
51
+ const path = __importStar(require("path"));
52
+ const parser_1 = require("../parser");
53
+ /**
54
+ * Global include stack shared across all loader instances.
55
+ * This is necessary because Nunjucks creates new loader instances for nested includes,
56
+ * but we need to track the entire include chain to detect circular dependencies.
57
+ *
58
+ * The stack is keyed by a unique compilation ID to support concurrent compilations.
59
+ */
60
+ const globalIncludeStacks = new Map();
61
+ /**
62
+ * Generate a unique compilation ID.
63
+ */
64
+ let compilationCounter = 0;
65
+ function generateCompilationId() {
66
+ return `compile-${++compilationCounter}-${Date.now()}`;
67
+ }
68
+ /**
69
+ * Custom Nunjucks loader that handles .prmd files specially.
70
+ *
71
+ * When a .prmd file is included, this loader:
72
+ * 1. Parses the frontmatter
73
+ * 2. Extracts only the body content
74
+ * 3. Returns the body for inclusion
75
+ *
76
+ * This allows prompts to be composed from other prompts without
77
+ * duplicating frontmatter or metadata.
78
+ */
79
+ class PrompdLoader extends nunjucks.Loader {
80
+ constructor(options) {
81
+ super();
82
+ // Required by Nunjucks loader interface - we use synchronous loading
83
+ this.async = false;
84
+ this.fileSystem = options.fileSystem;
85
+ this.baseDir = options.baseDir;
86
+ this.verbose = options.verbose || false;
87
+ this.maxDepth = options.maxDepth || 10;
88
+ this.parser = new parser_1.PrompdParser();
89
+ // Use provided compilation ID or generate a new one
90
+ // The compilation ID links all loader instances in a single compilation
91
+ this.compilationId = options.compilationId || generateCompilationId();
92
+ // Initialize global stack for this compilation if needed
93
+ if (!globalIncludeStacks.has(this.compilationId)) {
94
+ globalIncludeStacks.set(this.compilationId, new Set());
95
+ }
96
+ }
97
+ /**
98
+ * Get the include stack for this compilation.
99
+ */
100
+ getIncludeStack() {
101
+ return globalIncludeStacks.get(this.compilationId) || new Set();
102
+ }
103
+ /**
104
+ * Clean up the global stack after compilation is complete.
105
+ * Should be called when the top-level compilation finishes.
106
+ */
107
+ static cleanupCompilation(compilationId) {
108
+ globalIncludeStacks.delete(compilationId);
109
+ }
110
+ /**
111
+ * Get the source content for a template.
112
+ * This is the main entry point called by Nunjucks.
113
+ * Throws an error if the file is not found (required by Nunjucks interface).
114
+ */
115
+ getSource(name) {
116
+ // Resolve path relative to base directory
117
+ const resolvedPath = this.resolvePath(name);
118
+ const includeStack = this.getIncludeStack();
119
+ if (this.verbose) {
120
+ console.log(`[include] Resolving: ${name} -> ${resolvedPath}`);
121
+ }
122
+ // Check for circular includes
123
+ if (includeStack.has(resolvedPath)) {
124
+ const stack = Array.from(includeStack).join(' -> ');
125
+ throw new Error(`Circular include detected: ${stack} -> ${resolvedPath}`);
126
+ }
127
+ // Check max depth
128
+ if (includeStack.size >= this.maxDepth) {
129
+ throw new Error(`Maximum include depth (${this.maxDepth}) exceeded. Check for deep nesting or circular includes.`);
130
+ }
131
+ // Check if file exists
132
+ const exists = this.fileSystem.exists(resolvedPath);
133
+ if (typeof exists === 'object' && 'then' in exists) {
134
+ // Async not supported in getSource - throw helpful error
135
+ throw new Error('PrompdLoader requires synchronous file system operations. Use NodeFileSystem or ensure MemoryFileSystem is pre-populated.');
136
+ }
137
+ if (!exists) {
138
+ // Try with .prmd extension if not specified
139
+ if (!resolvedPath.endsWith('.prmd')) {
140
+ const withExt = resolvedPath + '.prmd';
141
+ const existsWithExt = this.fileSystem.exists(withExt);
142
+ if (existsWithExt === true) {
143
+ return this.loadFile(withExt);
144
+ }
145
+ }
146
+ // Nunjucks expects an error to be thrown for missing files
147
+ throw new Error(`Template not found: ${name} (resolved to ${resolvedPath})`);
148
+ }
149
+ return this.loadFile(resolvedPath);
150
+ }
151
+ /**
152
+ * Load and process a file.
153
+ * Note: We do NOT remove from stack after loading because Nunjucks will process
154
+ * the returned content which may contain more includes. The stack tracks the
155
+ * entire include chain during a single compilation.
156
+ */
157
+ loadFile(filePath) {
158
+ const includeStack = this.getIncludeStack();
159
+ // Add to include stack - this persists for the entire compilation
160
+ includeStack.add(filePath);
161
+ // Read file content
162
+ const content = this.fileSystem.readFile(filePath);
163
+ if (typeof content === 'object' && 'then' in content) {
164
+ throw new Error('PrompdLoader requires synchronous file system operations.');
165
+ }
166
+ // Process based on file extension
167
+ const ext = path.extname(filePath).toLowerCase();
168
+ let processedContent;
169
+ if (ext === '.prmd') {
170
+ // Parse .prmd file and extract body only
171
+ processedContent = this.processPrompdFile(content, filePath);
172
+ }
173
+ else {
174
+ // Return raw content for other file types
175
+ processedContent = content;
176
+ }
177
+ if (this.verbose) {
178
+ const preview = processedContent.substring(0, 50).replace(/\n/g, '\\n');
179
+ console.log(`[include] Loaded ${ext} file: ${filePath} (${preview}...)`);
180
+ }
181
+ return {
182
+ src: processedContent,
183
+ path: filePath,
184
+ noCache: true // Don't cache - files may change
185
+ };
186
+ }
187
+ /**
188
+ * Process a .prmd file: parse frontmatter and return only the body.
189
+ */
190
+ processPrompdFile(content, filePath) {
191
+ try {
192
+ const parsed = this.parser.parseContent(content);
193
+ if (this.verbose) {
194
+ const paramCount = parsed.metadata?.parameters?.length || 0;
195
+ console.log(`[include] Parsed .prmd: ${filePath} (${paramCount} params)`);
196
+ }
197
+ // Return just the body content (after frontmatter)
198
+ // If no body, return empty string
199
+ return parsed.content || '';
200
+ }
201
+ catch (error) {
202
+ // If parsing fails, log warning and return raw content
203
+ const errorMessage = error instanceof Error ? error.message : String(error);
204
+ if (this.verbose) {
205
+ console.log(`[include] Warning: Failed to parse ${filePath}: ${errorMessage}`);
206
+ }
207
+ // Fall back to raw content minus frontmatter
208
+ // Simple regex to strip frontmatter
209
+ const withoutFrontmatter = content.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '');
210
+ return withoutFrontmatter;
211
+ }
212
+ }
213
+ /**
214
+ * Resolve a path relative to the base directory.
215
+ */
216
+ resolvePath(name) {
217
+ // Handle absolute paths
218
+ if (path.isAbsolute(name)) {
219
+ return name;
220
+ }
221
+ // Handle relative paths (./file.prmd, ../file.prmd, file.prmd)
222
+ return this.fileSystem.join(this.baseDir, name);
223
+ }
224
+ /**
225
+ * Create a child loader for nested includes.
226
+ * The child loader shares the compilation ID to use the same include stack.
227
+ */
228
+ createChildLoader(newBaseDir) {
229
+ return new PrompdLoader({
230
+ fileSystem: this.fileSystem,
231
+ baseDir: newBaseDir,
232
+ verbose: this.verbose,
233
+ maxDepth: this.maxDepth,
234
+ compilationId: this.compilationId // Share the compilation ID for cycle detection
235
+ });
236
+ }
237
+ /**
238
+ * Get the compilation ID for this loader.
239
+ * Useful for cleanup after compilation.
240
+ */
241
+ getCompilationId() {
242
+ return this.compilationId;
243
+ }
244
+ }
245
+ exports.PrompdLoader = PrompdLoader;
246
+ /**
247
+ * Create a Nunjucks environment configured with the Prompd loader.
248
+ *
249
+ * @param options - Loader options
250
+ * @returns Configured Nunjucks environment
251
+ */
252
+ function createPrompdEnvironment(options) {
253
+ const loader = new PrompdLoader(options);
254
+ const env = new nunjucks.Environment(loader, {
255
+ autoescape: false,
256
+ throwOnUndefined: false,
257
+ trimBlocks: true,
258
+ lstripBlocks: true,
259
+ tags: {
260
+ blockStart: '{%',
261
+ blockEnd: '%}',
262
+ variableStart: '{{',
263
+ variableEnd: '}}',
264
+ commentStart: '{#',
265
+ commentEnd: '#}'
266
+ }
267
+ });
268
+ return env;
269
+ }
270
+ //# sourceMappingURL=prompd-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompd-loader.js","sourceRoot":"","sources":["../../../src/lib/compiler/prompd-loader.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmRH,0DAmBC;AApSD,mDAAqC;AACrC,2CAA6B;AAE7B,sCAAyC;AAEzC;;;;;;GAMG;AACH,MAAM,mBAA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D;QAC/D,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;IAED;;;OAGG;IACH,iBAAiB,CAAC,UAAkB;QAClC,OAAO,IAAI,YAAY,CAAC;YACtB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,OAAO,EAAE,UAAU;YACnB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,aAAa,EAAE,IAAI,CAAC,aAAa,CAAE,+CAA+C;SACnF,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,gBAAgB;QACd,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF;AA9MD,oCA8MC;AAED;;;;;GAKG;AACH,SAAgB,uBAAuB,CAAC,OAA4B;IAClE,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;IAEzC,MAAM,GAAG,GAAG,IAAI,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE;QAC3C,UAAU,EAAE,KAAK;QACjB,gBAAgB,EAAE,KAAK;QACvB,UAAU,EAAE,IAAI;QAChB,YAAY,EAAE,IAAI;QAClB,IAAI,EAAE;YACJ,UAAU,EAAE,IAAI;YAChB,QAAQ,EAAE,IAAI;YACd,aAAa,EAAE,IAAI;YACnB,WAAW,EAAE,IAAI;YACjB,YAAY,EAAE,IAAI;YAClB,UAAU,EAAE,IAAI;SACjB;KACF,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Section Override Processing
3
+ *
4
+ * Provides complete functionality for parsing, validating, and applying
5
+ * section-based content overrides in prompd template inheritance.
6
+ *
7
+ * This is a direct port of the Python CLI's SectionOverrideProcessor.
8
+ */
9
+ import { SectionInfo } from './types';
10
+ import { IFileSystem } from './file-system';
11
+ export declare class SectionOverrideProcessor {
12
+ private headingPattern;
13
+ private sectionIdPattern;
14
+ constructor();
15
+ /**
16
+ * Extract all sections from markdown content.
17
+ */
18
+ extractSections(content: string): Map<string, SectionInfo>;
19
+ /**
20
+ * Apply overrides and merge parent/child sections.
21
+ */
22
+ applyOverrides(parentSections: Map<string, SectionInfo>, childSections: Map<string, SectionInfo>, overrides: Record<string, string | null>, baseDir: string, verbose?: boolean, fileSystem?: IFileSystem): Promise<string>;
23
+ /**
24
+ * Load override content from a file.
25
+ */
26
+ loadOverrideContent(overridePath: string, baseDir: string, fileSystem?: IFileSystem): Promise<string>;
27
+ /**
28
+ * Resolve override path with security checks.
29
+ */
30
+ private resolveOverridePath;
31
+ /**
32
+ * Generate a section ID from heading text (kebab-case).
33
+ */
34
+ private generateSectionId;
35
+ /**
36
+ * Validate section ID format.
37
+ */
38
+ private validateSectionId;
39
+ }
40
+ //# sourceMappingURL=section-override.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"section-override.d.ts","sourceRoot":"","sources":["../../../src/lib/compiler/section-override.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,qBAAa,wBAAwB;IACnC,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,gBAAgB,CAAS;;IAUjC;;OAEG;IACH,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC;IA6F1D;;OAEG;IACG,cAAc,CAClB,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACxC,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,EACvC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,EACxC,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,OAAe,EACxB,UAAU,CAAC,EAAE,WAAW,GACvB,OAAO,CAAC,MAAM,CAAC;IAiElB;;OAEG;IACG,mBAAmB,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAiD3G;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAiC3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAUzB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAuB1B"}