Class: Aura::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/aura/analyzer.rb

Overview

Lightweight semantic pass over the node list. Catches the class of errors a context-free grammar can’t: referencing a model in ‘train`/`evaluate`/`route` that was never defined. Stays silent for valid programs (only raises), which keeps `Aura.transpile` free of side-effects.

Constant Summary collapse

KNOWN_PROVIDERS =
%i[openai ollama].freeze
HTTP_VERBS =
%w[get post put patch delete options head].freeze
TRAINABLE_KINDS =
%i[torch transfer].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Analyzer

Returns a new instance of Analyzer.



16
17
18
# File 'lib/aura/analyzer.rb', line 16

def initialize(nodes)
  @nodes = nodes
end

Class Method Details

.analyze(nodes) ⇒ Object



12
13
14
# File 'lib/aura/analyzer.rb', line 12

def self.analyze(nodes)
  new(nodes).analyze
end

Instance Method Details

#analyzeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aura/analyzer.rb', line 22

def analyze
  models         = @nodes.select { |n| n[:type] == :model }
  defined_models = models.map { |n| n[:name] }
  kinds          = models.each_with_object({}) { |n, h| h[n[:name]] = n[:kind] }
  check_unique_models!(defined_models)
  check_unique_routes!
  check_unique_environment!
  check_providers!
  check_http_verbs!

  @nodes.each do |node|
    case node[:type]
    when :train
      require_model!(defined_models, node[:model], "train")
      require_trainable!(kinds, node[:model], "train")
    when :evaluate
      require_model!(defined_models, node[:model], "evaluate")
      require_trainable!(kinds, node[:model], "evaluate")
    when :route
      require_model!(defined_models, node[:model], "route #{node[:path]}") if node[:model]
    end
  end

  @nodes
end