CommBus.Template.Loader (CommBus v0.1.0)

Copy Markdown View Source

Prompt template loader with YAML frontmatter.

Summary

Functions

Reads a prompt file from disk, parses its YAML frontmatter, validates the schema, and returns a %Prompt{} struct.

Parses a prompt from a raw string containing YAML frontmatter delimited by ---, validates the schema, and returns a %Prompt{} struct.

Recursively loads all .md prompt files under the given directory, returning a list of validated %Prompt{} structs.

Splits a prompt string on --- delimiters and parses the YAML frontmatter into a map.

Functions

load_prompt_file(path, opts \\ [])

@spec load_prompt_file(
  String.t(),
  keyword()
) ::
  {:ok, CommBus.Template.Prompt.t()}
  | {:error, [CommBus.Template.ValidationError.t()]}

Reads a prompt file from disk, parses its YAML frontmatter, validates the schema, and returns a %Prompt{} struct.

Parameters

  • path — Absolute or relative file path to a Markdown prompt file.
  • opts — Keyword options: :schema (:devman, :human, :flex), :root (prompt root directory for slug derivation).

Returns

{:ok, %Prompt{}} on success or {:error, [%ValidationError{}]} on failure.

load_prompt_string(content, path \\ nil, opts \\ [])

@spec load_prompt_string(String.t(), String.t() | nil, keyword()) ::
  {:ok, CommBus.Template.Prompt.t()}
  | {:error, [CommBus.Template.ValidationError.t()]}

Parses a prompt from a raw string containing YAML frontmatter delimited by ---, validates the schema, and returns a %Prompt{} struct.

Parameters

  • content — The full prompt string including YAML frontmatter.
  • path — Optional file path for error reporting and slug derivation.
  • opts — Keyword options: :schema, :root.

Returns

{:ok, %Prompt{}} on success or {:error, [%ValidationError{}]} on failure.

load_prompts(root, opts \\ [])

@spec load_prompts(
  String.t(),
  keyword()
) ::
  {:ok, [CommBus.Template.Prompt.t()]}
  | {:error, [CommBus.Template.ValidationError.t()]}

Recursively loads all .md prompt files under the given directory, returning a list of validated %Prompt{} structs.

Parameters

  • root — The root directory to scan for prompt files.
  • opts — Keyword options: :schema, :root.

Returns

{:ok, [%Prompt{}]} if all files pass validation, or {:error, [%ValidationError{}]} collecting errors from all invalid files.

parse_frontmatter(content, path \\ nil)

@spec parse_frontmatter(String.t(), String.t() | nil) ::
  {:ok, map(), String.t()} | {:error, [CommBus.Template.ValidationError.t()]}

Splits a prompt string on --- delimiters and parses the YAML frontmatter into a map.

Parameters

  • content — The full prompt string.
  • path — Optional file path for error reporting.

Returns

{:ok, frontmatter_map, body_string} on success or {:error, [%ValidationError{}]} on failure.