Module: Braintrust::Internal::Template

Defined in:
lib/braintrust/internal/template.rb

Overview

Template rendering utilities for Mustache templates

Class Method Summary collapse

Class Method Details

.find_missing_variables(text, variables) ⇒ Array<String>

Find Mustache variables in template that are not provided

Parameters:

  • text (String)

    Template text

  • variables (Hash)

    Available variables

Returns:

  • (Array<String>)

    List of missing variable names



51
52
53
54
55
56
57
# File 'lib/braintrust/internal/template.rb', line 51

def find_missing_variables(text, variables)
  # Extract {{variable}} and {{variable.path}} patterns
  # Mustache uses {{name}} syntax
  text.scan(/\{\{([^}#^\/!>]+)\}\}/).flatten.map(&:strip).uniq.reject do |var|
    resolve_variable(var, variables)
  end
end

.render(text, variables, format:, strict: false) ⇒ String

Render a template string with variable substitution

Parameters:

  • text (String)

    Template text to render

  • variables (Hash)

    Variables to substitute

  • format (String)

    Template format: “mustache”, “none”, or “nunjucks”

  • strict (Boolean) (defaults to: false)

    Raise error on missing variables (default: false)

Returns:

  • (String)

    Rendered text



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/braintrust/internal/template.rb', line 18

def render(text, variables, format:, strict: false)
  return text unless text.is_a?(String)

  case format
  when "none"
    # No templating - return text unchanged
    text
  when "nunjucks"
    # Nunjucks is a UI-only feature in Braintrust
    raise Error, "Nunjucks templates are not supported in the Ruby SDK. " \
                 "Nunjucks only works in Braintrust playgrounds. " \
                 "Please use 'mustache' or 'none' template format, or invoke the prompt via the API proxy."
  when "mustache", "", nil
    # Default: Mustache templating
    if strict
      missing = find_missing_variables(text, variables)
      if missing.any?
        raise Error, "Missing required variables: #{missing.join(", ")}"
      end
    end

    Vendor::Mustache.render(text, variables)
  else
    raise Error, "Unknown template format: #{format.inspect}. " \
                 "Supported formats are 'mustache' and 'none'."
  end
end

.resolve_variable(path, variables) ⇒ Object?

Check if a variable path exists in a hash

Parameters:

  • path (String)

    Dot-separated variable path (e.g., “user.name”)

  • variables (Hash)

    Variables to search

Returns:

  • (Object, nil)

    The value if found, nil otherwise



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/braintrust/internal/template.rb', line 64

def resolve_variable(path, variables)
  parts = path.split(".")
  value = variables

  parts.each do |part|
    return nil unless value.is_a?(Hash)
    # Try both string and symbol keys
    value = value[part] || value[part.to_sym]
    return nil if value.nil?
  end

  value
end

.stringify_keys(hash) ⇒ Hash

Convert hash keys to strings recursively

Parameters:

  • hash (Hash)

    Hash with symbol or string keys

Returns:

  • (Hash)

    Hash with all string keys



82
83
84
85
86
87
88
# File 'lib/braintrust/internal/template.rb', line 82

def stringify_keys(hash)
  return {} unless hash.is_a?(Hash)

  hash.transform_keys(&:to_s).transform_values do |v|
    v.is_a?(Hash) ? stringify_keys(v) : v
  end
end