Skip to main content

@flow-state-dev/engine/prompt-file

Interfaces​

LoadPromptFileOptions​

Options for loadPromptFile.

Properties​

filters?​
filters?: PromptFileFilters;

Custom Liquid filters, scoped to this PromptFile.

partialsDir?​
partialsDir?: string;

Directory to discover partial .md files in. Defaults to the directory of the loaded prompt file. Every .md file in this directory (excluding the prompt file itself) becomes a partial under its filename without .md.


PromptLoaderOptions​

Options for createPromptLoader.

Properties​

filters?​
filters?: PromptFileFilters;

Liquid filters applied to every prompt this loader reads.

partialsDir?​
partialsDir?: string;

Shared partials directory for every prompt this loader reads. Defaults to each prompt's own directory (the loadPromptFile default). Set this once when many prompts pull from a common _partials dir.


ResolveBaseDirOptions​

Options for resolveBaseDir.

Properties​

expect?​
expect?: string;

Relative path that must exist inside a candidate for it to qualify. Guards against bundler-rewritten module URLs that resolve to an existing directory inside the build output (e.g. .next/) — such a directory exists but won't contain the expected content. Defaults to accepting any existing candidate directory.

Type Aliases​

PromptLoader​

type PromptLoader = (relPath, perCallOptions?) => PromptFile;

A loader returned by createPromptLoader: resolves relPath against the captured base directory and loads the prompt. Per-call filters merge over (and override) the loader's shared filters.

Parameters​

relPath​

string

perCallOptions?​
filters?​

PromptFileFilters

Returns​

PromptFile

Functions​

createPromptLoader()​

function createPromptLoader(baseDir, options?): PromptLoader;

Build a prompt loader anchored at baseDir, so call sites drop the repeated importerUrl (import.meta.url) argument and shared partialsDir / filters:

const PROMPT_ROOT = resolveBaseDir(
[moduleDir(import.meta.url, "./prompts"), path.resolve(process.cwd(), "src/prompts")],
{ expect: "_partials" },
);
const load = createPromptLoader(PROMPT_ROOT, {
partialsDir: path.join(PROMPT_ROOT, "_partials"),
});
const analyst = load("analyst.prompt.md");

Resolution rule: baseDir must be an absolute directory path; relPath is joined onto it, yielding an absolute path — so resolution never depends on the invoker's working directory or on import.meta.url at load-call time. Compute baseDir once with resolveBaseDir: module-relative candidate first (real import.meta.url under node/tsx/vitest from any cwd), process.cwd()-derived fallback for bundled runtimes (e.g. Next.js, where Turbopack rewrites import.meta.url but dev/build pin cwd to the app package).

Parameters​

baseDir​

string

options?​

PromptLoaderOptions

Returns​

PromptLoader

Throws​

when baseDir is not absolute.


loadPromptFile()​

function loadPromptFile(
specifier,
importerUrl,
options?
): PromptFile;

Load and parse a .md prompt file relative to the calling module.

specifier is normally module-relative ("./analyst.prompt.md"), resolved against importerUrl (import.meta.url). An absolute path is also accepted and used as-is (importerUrl is then ignored) — useful where bundling makes import.meta.url unreliable (e.g. Next.js). Compute that absolute anchor with resolveBaseDir rather than bare process.cwd(), so resolution doesn't depend on the invoker's working directory.

Parameters​

specifier​

string

Module-relative path, or an absolute path to the prompt file.

importerUrl​

string

The caller's import.meta.url (ignored for absolute specifiers).

options?​

LoadPromptFileOptions

Custom filters and an optional partials directory override.

Returns​

PromptFile

Throws​

PromptFileLoadError when the file cannot be read.

Throws​

PromptFileParseError (via parsePromptFile) on invalid content.


moduleDir()​

function moduleDir(importerUrl, relative?): string | undefined;

Directory of the module identified by importerUrl (normally the caller's import.meta.url), optionally walked by relative.

Returns undefined when importerUrl is not a usable file: URL — bundlers (e.g. Turbopack) rewrite import.meta.url to virtual schemes, so callers compose this with resolveBaseDir and an absolute fallback rather than using the result directly. Node resolves ES modules at their realpath (pnpm-linked packages included), so the returned directory — and any walk relative to it — stays inside the package's real location regardless of how the package was symlinked.

Parameters​

importerUrl​

string

The caller's import.meta.url.

relative?​

string

Optional path from the module's directory to the wanted directory (e.g. ".."). Defaults to the module's own dir.

Returns​

string | undefined


resolveBaseDir()​

function resolveBaseDir(candidates, options?): string;

First candidate directory that exists (and contains expect, when given).

undefined entries are skipped, so moduleDir results compose directly — the standard anchoring idiom is a module-relative candidate first, then an explicit process.cwd()-derived fallback for bundled runtimes that pin the working directory (Next.js dev/build):

const APP_ROOT = resolveBaseDir(
[moduleDir(import.meta.url, "../.."), process.cwd()],
{ expect: "src/prompts" },
);

Parameters​

candidates​

readonly (string | undefined)[]

options?​

ResolveBaseDirOptions

Returns​

string

Throws​

when any defined candidate is not an absolute path — checked eagerly for every candidate before probing, so a malformed fallback fails in every runtime, not only in the runtime where the earlier candidates happen to miss.

Throws​

when no candidate qualifies; the message lists every candidate with the reason it was rejected.