|
1 | | -import Handlebars from 'handlebars'; |
2 | 1 | import {readFileSync} from 'fs'; |
3 | | -import path from 'path'; |
| 2 | +import Handlebars from 'handlebars'; |
| 3 | +import path, {dirname, resolve} from 'path'; |
4 | 4 | import {UserFacingError} from '../utils/errors.js'; |
5 | 5 |
|
6 | 6 | function initializeHandlebars() { |
7 | 7 | Handlebars.registerHelper('neq', (a, b) => a !== b); |
8 | | - Handlebars.registerPartial('embed', (ctx: {rootDir: string | null; file?: string}) => { |
| 8 | + Handlebars.registerPartial('embed', (ctx: {containingFile: string | null; file?: string}) => { |
9 | 9 | if (!ctx.file) { |
10 | 10 | throw new UserFacingError('file= is required'); |
11 | 11 | } |
12 | | - if (!ctx.rootDir) { |
13 | | - throw new UserFacingError('Cannot use `embed` if a rootDir is not specified'); |
| 12 | + if (!ctx.containingFile) { |
| 13 | + throw new UserFacingError('Cannot use `embed` if not `containingFile` is specified'); |
14 | 14 | } |
15 | 15 |
|
16 | | - const fullPath = path.join(ctx.rootDir, ctx.file); |
17 | | - const content = readFileSync(fullPath, 'utf8'); |
| 16 | + const fullPath = path.join(dirname(ctx.containingFile), ctx.file); |
| 17 | + let content = readFileSync(fullPath, 'utf8'); |
| 18 | + content = processAtFileReferencesSync(content, ctx.containingFile); |
18 | 19 |
|
19 | 20 | // Recursively support `embed`. |
20 | 21 | return Handlebars.compile(content, {strict: true})({ |
21 | 22 | ...ctx, |
22 | | - rootDir: path.dirname(fullPath), |
| 23 | + containingFile: fullPath, |
23 | 24 | }); |
24 | 25 | }); |
25 | 26 | } |
26 | 27 |
|
27 | 28 | initializeHandlebars(); |
28 | 29 |
|
29 | | -/** Renders the given content via Handlebars. */ |
30 | | -export function renderHandlebarsTemplate<T extends {rootDir: string | null}>( |
| 30 | +/** |
| 31 | + * Renders the given prompt template, by supporting: |
| 32 | + * - Handlebars builtins. |
| 33 | + * - Handlebars `embed` custom partials for including other files. |
| 34 | + * - Supporting the ecosystem `@<file-path>` standard for including other files. |
| 35 | + * |
| 36 | + * If `context` does not specify a `containingFile`, then other files cannot be embedded. |
| 37 | + */ |
| 38 | +export function renderPromptTemplate<T extends {containingFile: string | null}>( |
31 | 39 | content: string, |
32 | 40 | ctx: T, |
33 | 41 | ) { |
| 42 | + content = ctx.containingFile ? processAtFileReferencesSync(content, ctx.containingFile) : content; |
| 43 | + |
34 | 44 | const template = Handlebars.compile(content, {strict: true}); |
35 | 45 | const contextFiles: string[] = []; |
36 | 46 | const result = template(ctx, { |
@@ -72,3 +82,35 @@ export function renderHandlebarsTemplate<T extends {rootDir: string | null}>( |
72 | 82 | contextFiles, |
73 | 83 | }; |
74 | 84 | } |
| 85 | + |
| 86 | +function processAtFileReferencesSync(content: string, containingFile: string): string { |
| 87 | + let newContent = content; |
| 88 | + let match; |
| 89 | + // Match all `@./<file-path>` or `@/<file-path>` occurrences. |
| 90 | + // If someone intends to write such text in their prompt, they could overcome this |
| 91 | + // by indenting the string, or adding arbitrary characters before front. |
| 92 | + const regex = /^@(\.?\/[^\s]+)/gm; |
| 93 | + const containingFileDir = dirname(containingFile); |
| 94 | + |
| 95 | + while ((match = regex.exec(newContent)) !== null) { |
| 96 | + const filePath = match[1]; |
| 97 | + const fullPath = resolve(containingFileDir, filePath); |
| 98 | + let replacement: string; |
| 99 | + try { |
| 100 | + replacement = readFileSync(fullPath, 'utf8'); |
| 101 | + // Note: If we start searching the match start, where the new embedded content begins, |
| 102 | + // we can trivially. process nested embeds via the `@` syntax. |
| 103 | + } catch (e) { |
| 104 | + throw new Error( |
| 105 | + `Unexpected error while embedding \`${match[0]}\` reference in ${containingFile}. ` + |
| 106 | + `Error: ${e}`, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + newContent = |
| 111 | + newContent.substring(0, match.index) + |
| 112 | + processAtFileReferencesSync(replacement, fullPath) + |
| 113 | + newContent.substring(regex.lastIndex); |
| 114 | + } |
| 115 | + return newContent; |
| 116 | +} |
0 commit comments