Module: Ace::Core::Atoms::TemplateParser

Defined in:
lib/ace/core/atoms/template_parser.rb

Overview

Pure template parsing functions for context configurations

Constant Summary collapse

VALID_KEYS =

Valid template configuration keys

%w[
  files commands format embed_document_source
  include exclude output max_lines max_size timeout
].freeze

Class Method Summary collapse

Class Method Details

.extract_from_agent(content) ⇒ Hash?

Extract configuration from agent markdown files

Parameters:

  • content (String)

    Agent markdown content

Returns:

  • (Hash, nil)

    Extracted configuration or nil



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ace/core/atoms/template_parser.rb', line 59

def extract_from_agent(content)
  return nil if content.nil?

  # Look for Context Definition section
  context_match = content.match(/^## Context Definition\s*\n(.*?)(?=^## |\z)/m)
  return nil unless context_match

  context_section = context_match[1].strip

  # Extract YAML from code block
  yaml_match = context_section.match(/```(?:yaml|yml)?\s*\n(.*?)\n```/m)
  return nil unless yaml_match

  yaml_content = yaml_match[1]
  parse_yaml(yaml_content)
end

.extract_from_markdown(content) ⇒ Hash?

Extract configuration from markdown with <context-tool-config> tags

Parameters:

  • content (String)

    Markdown content

Returns:

  • (Hash, nil)

    Extracted configuration or nil



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ace/core/atoms/template_parser.rb', line 43

def extract_from_markdown(content)
  return nil if content.nil?

  # Look for <context-tool-config> block
  pattern = /<context-tool-config>\s*\n(.*?)\n<\/context-tool-config>/m
  match = content.match(pattern)

  return nil unless match

  yaml_content = match[1]
  parse_yaml(yaml_content)
end

.merge_configs(*configs) ⇒ Hash

Merge multiple configurations

Parameters:

  • configs (Array<Hash>)

    Configurations to merge

Returns:

  • (Hash)

    Merged configuration



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ace/core/atoms/template_parser.rb', line 157

def merge_configs(*configs)
  configs = configs.flatten.compact

  return {} if configs.empty?

  result = {
    "files" => [],
    "commands" => [],
    "include" => [],
    "exclude" => []
  }

  configs.each do |config|
    next unless config.is_a?(Hash)

    # Concatenate arrays
    %w[files commands include exclude].each do |key|
      result[key].concat(to_array(config[key]))
    end

    # Take last non-nil value for other keys
    %w[format embed_document_source output max_lines max_size timeout].each do |key|
      result[key] = config[key] if config.key?(key)
    end
  end

  # Remove duplicates from arrays
  %w[files commands include exclude].each do |key|
    result[key].uniq!
  end

  result.compact
end

.normalize_config(config) ⇒ Hash

Normalize configuration values

Parameters:

  • config (Hash)

    Configuration to normalize

Returns:

  • (Hash)

    Normalized configuration



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ace/core/atoms/template_parser.rb', line 123

def normalize_config(config)
  {
    "files" => to_array(config["files"]),
    "commands" => to_array(config["commands"]),
    "include" => to_array(config["include"]),
    "exclude" => to_array(config["exclude"]),
    "format" => config["format"],
    "embed_document_source" => config["embed_document_source"],
    "output" => config["output"],
    "max_lines" => config["max_lines"],
    "max_size" => config["max_size"],
    "timeout" => config["timeout"]
  }.compact
end

.parse(content) ⇒ Hash

Parse template configuration from string

Parameters:

  • content (String)

    Template content (YAML or markdown with embedded YAML)

Returns:

  • (Hash)

    Boolean, config: Hash, error: String



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ace/core/atoms/template_parser.rb', line 21

def parse(content)
  return {success: false, error: "Content cannot be nil"} if content.nil?
  return {success: false, error: "Content cannot be empty"} if content.strip.empty?

  # Try to extract from markdown with tags first
  config = extract_from_markdown(content)

  # If not found, try to parse as direct YAML
  config ||= parse_yaml(content)

  if config.nil?
    return {success: false, error: "No valid configuration found"}
  end

  validate_config(config)
rescue => e
  {success: false, error: "Failed to parse template: #{e.message}"}
end

.parse_yaml(yaml_string) ⇒ Hash?

Parse YAML string to hash

Parameters:

  • yaml_string (String)

    YAML content

Returns:

  • (Hash, nil)

    Parsed configuration or nil



79
80
81
82
83
84
85
86
87
88
# File 'lib/ace/core/atoms/template_parser.rb', line 79

def parse_yaml(yaml_string)
  return nil if yaml_string.nil? || yaml_string.strip.empty?

  result = YAML.safe_load(yaml_string, permitted_classes: [Symbol])

  # Ensure it's a hash
  result.is_a?(Hash) ? stringify_keys(result) : nil
rescue Psych::SyntaxError
  nil
end

.stringify_keys(hash) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/ace/core/atoms/template_parser.rb', line 212

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

  hash.each_with_object({}) do |(key, value), result|
    result[key.to_s] = value.is_a?(Hash) ? stringify_keys(value) : value
  end
end

.template?(content) ⇒ Boolean

Check if content appears to be a template

Parameters:

  • content (String)

    Content to check

Returns:

  • (Boolean)

    true if content looks like a template



194
195
196
197
198
199
200
201
202
203
# File 'lib/ace/core/atoms/template_parser.rb', line 194

def template?(content)
  return false if content.nil?

  # Check for various template indicators
  content.include?("<context-tool-config>") ||
    content.match?(/^files:\s*$/m) ||
    content.match?(/^commands:\s*$/m) ||
    content.match?(/^include:\s*$/m) ||
    content.match?(/^## Context Definition/m)
end

.to_array(value) ⇒ Array

Convert value to array

Parameters:

  • value (nil, String, Array)

    Value to convert

Returns:

  • (Array)

    Array of strings



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ace/core/atoms/template_parser.rb', line 141

def to_array(value)
  case value
  when nil
    []
  when Array
    value.map(&:to_s)
  when String
    [value]
  else
    [value.to_s]
  end
end

.validate_config(config) ⇒ Hash

Validate configuration structure

Parameters:

  • config (Hash)

    Configuration to validate

Returns:

  • (Hash)

    Boolean, config: Hash, error: String



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ace/core/atoms/template_parser.rb', line 93

def validate_config(config)
  return {success: false, error: "Config must be a Hash"} unless config.is_a?(Hash)

  # Check for unknown keys
  unknown_keys = config.keys - VALID_KEYS
  unless unknown_keys.empty?
    return {
      success: false,
      error: "Unknown configuration keys: #{unknown_keys.join(", ")}"
    }
  end

  # Normalize arrays
  normalized = normalize_config(config)

  # Validate required content
  if normalized["files"].empty? && normalized["commands"].empty? &&
      normalized["include"].empty?
    return {
      success: false,
      error: "Configuration must specify 'files', 'commands', or 'include'"
    }
  end

  {success: true, config: normalized}
end