Module: Mana::Introspect
- Defined in:
- lib/mana/introspect.rb
Overview
Introspects the caller's source file to discover user-defined methods.
Uses Prism AST to extract def nodes with their parameter signatures,
descriptions (from comments above the def), and parameter types (from YARD @param tags).
Class Method Summary collapse
-
.format_for_prompt(methods) ⇒ Object
Format discovered methods as a string for the system prompt.
-
.methods_from_file(path) ⇒ Object
Extract method definitions from a Ruby source file.
Class Method Details
.format_for_prompt(methods) ⇒ Object
Format discovered methods as a string for the system prompt. Includes descriptions when available.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mana/introspect.rb', line 39 def format_for_prompt(methods) return "" if methods.empty? lines = methods.map do |m| sig = m[:params].empty? ? m[:name] : "#{m[:name]}(#{m[:params].join(', ')})" if m[:description] " #{sig} — #{m[:description]}" else " #{sig}" end end "Available Ruby functions:\n#{lines.join("\n")}" end |
.methods_from_file(path) ⇒ Object
Extract method definitions from a Ruby source file. Returns an array of { name:, params:, description:, param_types: } hashes.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mana/introspect.rb', line 13 def methods_from_file(path) return [] unless path && File.exist?(path) source = File.read(path) source_lines = source.lines result = Prism.parse(source) methods = [] walk(result.value) do |node| next unless node.is_a?(Prism::DefNode) params = extract_params(node) description, param_types = extract_comments(node, source_lines) methods << { name: node.name.to_s, params: params, description: description, param_types: param_types } end methods end |