Skip to content

Commit d2c0617

Browse files
authored
fix(js/ai/prompts): fix prompt loading on node 18 (#1738)
1 parent bae3a2c commit d2c0617

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

js/ai/src/prompt.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -662,36 +662,48 @@ export function loadPromptFolder(
662662
): void {
663663
const promptsPath = resolve(dir);
664664
if (existsSync(promptsPath)) {
665-
const dirEnts = readdirSync(promptsPath, {
666-
withFileTypes: true,
667-
recursive: true,
668-
});
669-
for (const dirEnt of dirEnts) {
670-
const parentPath = dirEnt.parentPath ?? dirEnt.path;
671-
if (dirEnt.isFile() && dirEnt.name.endsWith('.prompt')) {
672-
if (dirEnt.name.startsWith('_')) {
673-
const partialName = dirEnt.name.substring(1, dirEnt.name.length - 7);
674-
definePartial(
675-
registry,
676-
partialName,
677-
readFileSync(join(parentPath, dirEnt.name), {
678-
encoding: 'utf8',
679-
})
680-
);
681-
logger.debug(
682-
`Registered Dotprompt partial "${partialName}" from "${join(parentPath, dirEnt.name)}"`
683-
);
684-
} else {
685-
// If this prompt is in a subdirectory, we need to include that
686-
// in the namespace to prevent naming conflicts.
687-
let prefix = '';
688-
let name = dirEnt.name;
689-
if (promptsPath !== parentPath) {
690-
prefix = parentPath.replace(`${promptsPath}/`, '') + '/';
691-
}
692-
loadPrompt(registry, promptsPath, name, prefix, ns);
693-
}
665+
loadPromptFolderRecursively(registry, dir, ns, '');
666+
}
667+
}
668+
export function loadPromptFolderRecursively(
669+
registry: Registry,
670+
dir: string,
671+
ns: string,
672+
subDir: string
673+
): void {
674+
const promptsPath = resolve(dir);
675+
const dirEnts = readdirSync(join(promptsPath, subDir), {
676+
withFileTypes: true,
677+
});
678+
for (const dirEnt of dirEnts) {
679+
const parentPath = join(promptsPath, subDir);
680+
let fileName = dirEnt.name;
681+
if (dirEnt.isFile() && fileName.endsWith('.prompt')) {
682+
if (fileName.startsWith('_')) {
683+
const partialName = fileName.substring(1, fileName.length - 7);
684+
definePartial(
685+
registry,
686+
partialName,
687+
readFileSync(join(parentPath, fileName), {
688+
encoding: 'utf8',
689+
})
690+
);
691+
logger.debug(
692+
`Registered Dotprompt partial "${partialName}" from "${join(parentPath, fileName)}"`
693+
);
694+
} else {
695+
// If this prompt is in a subdirectory, we need to include that
696+
// in the namespace to prevent naming conflicts.
697+
loadPrompt(
698+
registry,
699+
promptsPath,
700+
fileName,
701+
subDir ? `${subDir}/` : '',
702+
ns
703+
);
694704
}
705+
} else if (dirEnt.isDirectory()) {
706+
loadPromptFolderRecursively(registry, dir, ns, join(subDir, fileName));
695707
}
696708
}
697709
}
@@ -726,7 +738,7 @@ function loadPrompt(
726738
name = parts[0];
727739
variant = parts[1];
728740
}
729-
const source = readFileSync(join(path, (prefix ?? '') + filename), 'utf8');
741+
const source = readFileSync(join(path, prefix ?? '', filename), 'utf8');
730742
const parsedPrompt = registry.dotprompt.parse(source);
731743
definePromptAsync(
732744
registry,

0 commit comments

Comments
 (0)