Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively look for .prompt files in specified dir #234

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions js/plugins/dotprompt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import { loadPromptFolder, lookupPrompt } from './registry.js';
export { defineDotprompt, Dotprompt };

export interface DotpromptPluginOptions {
// Directory to look for .prompt files.
//
// Note: This directory will be searched recursively, and any sub-directory
// paths will be included in the prompt name. E.g. - if a prompt file is
// located at `<dir>/foo/bar.prompt`, the prompt name will be `foo-bar`.
dir: string;
}

Expand Down
22 changes: 17 additions & 5 deletions js/plugins/dotprompt/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,23 @@ export async function loadPromptFolder(
promptsPath,
{
withFileTypes: true,
recursive: false,
recursive: true,
tonybaroneee marked this conversation as resolved.
Show resolved Hide resolved
},
(err, dirEnts) => {
if (err) {
reject(err);
} else {
dirEnts.forEach(async (dirEnt) => {
if (dirEnt.isFile() && dirEnt.name.endsWith('.prompt')) {
loadPrompt(dirEnt.path, dirEnt.name);
// If this prompt is in a subdirectory, we need to include that
// in the namespace to prevent naming conflicts.
let prefix = '';
if (promptsPath !== dirEnt.path) {
prefix = dirEnt.path
.replace(`${promptsPath}/`, '')
.replace(/\//g, '-');
}
loadPrompt(dirEnt.path, dirEnt.name, prefix);
}
});
resolve();
Expand All @@ -101,8 +109,12 @@ export async function loadPromptFolder(
});
}

export function loadPrompt(path: string, filename: string): Dotprompt {
let name = basename(filename, '.prompt');
export function loadPrompt(
path: string,
filename: string,
prefix = ''
): Dotprompt {
let name = `${prefix ? `${prefix}-` : ''}${basename(filename, '.prompt')}`;
let variant: string | null = null;
if (name.includes('.')) {
const parts = name.split('.');
Expand All @@ -114,6 +126,6 @@ export function loadPrompt(path: string, filename: string): Dotprompt {
if (variant) {
prompt.variant = variant;
}
prompt.define({ ns: 'dotprompt' });
prompt.define({ ns: `dotprompt` });
return prompt;
}
Loading