Class: PromptEngine::VariableDetector

Inherits:
Object
  • Object
show all
Defined in:
app/services/prompt_engine/variable_detector.rb

Constant Summary collapse

VARIABLE_PATTERN =

Regex pattern to match {variable_name} syntax Allows letters, numbers, underscores, and dots for nested variables

/\{\{([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)\}\}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ VariableDetector

Returns a new instance of VariableDetector.



9
10
11
# File 'app/services/prompt_engine/variable_detector.rb', line 9

def initialize(content)
  @content = content.to_s
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'app/services/prompt_engine/variable_detector.rb', line 7

def content
  @content
end

Instance Method Details

#extract_variablesObject

Extract all variables from the content



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/prompt_engine/variable_detector.rb', line 14

def extract_variables
  variables = []

  content.scan(VARIABLE_PATTERN) do |match|
    variable_name = match.first
    variables << {
      name: variable_name,
      placeholder: "{{#{variable_name}}}",
      position: Regexp.last_match.offset(0),
      type: infer_type(variable_name),
      required: true
    }
  end

  # Remove duplicates while preserving order
  variables.uniq { |v| v[:name] }
end

#has_variables?Boolean

Check if content contains variables

Returns:

  • (Boolean)


38
39
40
# File 'app/services/prompt_engine/variable_detector.rb', line 38

def has_variables?
  content.match?(VARIABLE_PATTERN)
end

#render(variables = {}) ⇒ Object

Replace variables with provided values



48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/prompt_engine/variable_detector.rb', line 48

def render(variables = {})
  rendered = content.dup

  variables.each do |key, value|
    # Support both string and symbol keys
    placeholder = "{{#{key}}}"
    rendered.gsub!(placeholder, value.to_s)
  end

  rendered
end

#validate_variables(provided_variables = {}) ⇒ Object

Validate that all required variables are provided



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/prompt_engine/variable_detector.rb', line 61

def validate_variables(provided_variables = {})
  missing = []
  stringified_keys = provided_variables.stringify_keys

  variable_names.each do |var_name|
    unless stringified_keys.key?(var_name)
      missing << var_name
    end
  end

  {
    valid: missing.empty?,
    missing_variables: missing
  }
end

#variable_countObject

Count unique variables



43
44
45
# File 'app/services/prompt_engine/variable_detector.rb', line 43

def variable_count
  variable_names.count
end

#variable_namesObject

Get just the variable names as an array



33
34
35
# File 'app/services/prompt_engine/variable_detector.rb', line 33

def variable_names
  extract_variables.map { |v| v[:name] }
end