| 1 | import { v4 as uuidv4 } from "uuid"; |
| 2 | |
| 3 | export type HandlebarsType = typeof import("handlebars"); |
| 4 | |
| 5 | function convertToLetter(num: number): string { |
| 6 | let result = ""; |
| 7 | while (num > 0) { |
| 8 | const remainder = (num - 1) % 26; |
| 9 | result = String.fromCharCode(97 + remainder) + result; |
| 10 | num = Math.floor((num - 1) / 26); |
| 11 | } |
| 12 | return result; |
| 13 | } |
| 14 | |
| 15 | export function registerHelpers( |
| 16 | handlebars: HandlebarsType, |
| 17 | helpers: Array<[string, Handlebars.HelperDelegate]>, |
| 18 | ): { |
| 19 | [key: string]: Promise<string>; |
| 20 | } { |
| 21 | const promises: { [key: string]: Promise<string> } = {}; |
| 22 | |
| 23 | for (const [name, helper] of helpers) { |
| 24 | handlebars.registerHelper(name, (...args) => { |
| 25 | const id = uuidv4(); |
| 26 | promises[id] = helper(...args); |
| 27 | return `__${id}__`; |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | return promises; |
| 32 | } |
| 33 | |
| 34 | export async function prepareTemplatedFilepaths( |
| 35 | handlebars: HandlebarsType, |
| 36 | template: string, |
| 37 | inputData: Record<string, string>, |
| 38 | ctxProviderNames: string[], |
| 39 | readFile: (filepath: string) => Promise<string>, |
| 40 | getUriFromPath: (path: string) => Promise<string | undefined>, |
| 41 | ) { |
| 42 | // First, replace filepaths with letters to avoid escaping issues |
| 43 | const ast = handlebars.parse(template); |
| 44 | |
| 45 | const filepathLetters: Map<string, string> = new Map(); |
| 46 | const requiredContextProviders: Set<string> = new Set(); |
| 47 | let withLetterKeys = template; |
| 48 | |
| 49 | let letterIndex = 1; |
| 50 | |
| 51 | for (const i in ast.body) { |
| 52 | const node = ast.body[i] as any; |
| 53 | |
| 54 | if (node.type === "MustacheStatement") { |
| 55 | const originalNodeVal = node.path.original; |
| 56 | if (originalNodeVal.toLowerCase() === "input") { |
| 57 | continue; |
| 58 | } |
| 59 | const isFilepath = !ctxProviderNames.includes(originalNodeVal); |
| 60 | |
| 61 | if (isFilepath) { |
| 62 | const letter = convertToLetter(letterIndex); |
| 63 | |
| 64 | filepathLetters.set(letter, originalNodeVal); |
| 65 | |
| 66 | withLetterKeys = withLetterKeys.replace( |
| 67 | new RegExp(`{{\\s*${originalNodeVal}\\s*}}`), |
| 68 | `{{${letter}}}`, |
| 69 | ); |
| 70 | |
| 71 | letterIndex++; |
| 72 | } else { |
| 73 | requiredContextProviders.add(originalNodeVal); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Then, resolve the filepaths to their actual content and add to template data |
| 79 | // Fallback to simple error string if file read fails |
| 80 | const templateData = { ...inputData }; |
| 81 | |
| 82 | for (const [letter, filepath] of filepathLetters.entries()) { |
| 83 | try { |
| 84 | const uri = await getUriFromPath(filepath); |
| 85 | if (uri) { |
| 86 | const fileContents = await readFile(uri); |
| 87 | templateData[letter] = fileContents; |
| 88 | } else { |
| 89 | throw new Error(`File not found: ${filepath}`); |
| 90 | } |
| 91 | } catch (e) { |
| 92 | console.error(`Error reading file in prompt file ${filepath}:`, e); |
| 93 | templateData[letter] = `[Error reading file "${filepath}"]`; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return { withLetterKeys, templateData, requiredContextProviders }; |
| 98 | } |
| 99 | |
| 100 | export async function resolveHelperPromises( |
| 101 | renderedString: string, |
| 102 | promises: { [key: string]: Promise<string> }, |
| 103 | ): Promise<string> { |
| 104 | await Promise.all(Object.values(promises)); |
| 105 | |
| 106 | for (const id in promises) { |
| 107 | renderedString = renderedString.replace(`__${id}__`, await promises[id]); |
| 108 | } |
| 109 | |
| 110 | return renderedString; |
| 111 | } |