Module: LanguageOperator::CLI::Commands::Agent::Helpers::CodeParser

Included in:
Base
Defined in:
lib/language_operator/cli/commands/agent/helpers/code_parser.rb

Overview

Helper methods for parsing agent code from ConfigMaps

Instance Method Summary collapse

Instance Method Details

#extract_hash_value(code, key) ⇒ Hash

Extract a hash value from DSL code (e.g., inputs: { foo: 'bar' })

Parameters:

  • Code snippet

  • Key to extract

Returns:

  • Extracted hash or empty hash



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/language_operator/cli/commands/agent/helpers/code_parser.rb', line 96

def extract_hash_value(code, key)
  match = code.match(/#{key}:\s*\{([^}]*)\}/)
  return {} unless match

  hash_content = match[1].strip
  return {} if hash_content.empty?

  # Parse simple key: 'value' or key: "value" pairs
  result = {}
  hash_content.scan(/(\w+):\s*(['"])([^'"]*)\2/) do |k, _quote, v|
    result[k.to_sym] = v
  end
  result
end

#extract_string_value(code, key) ⇒ String

Extract a string value from DSL code (e.g., instructions: "...")

Parameters:

  • Code snippet

  • Key to extract

Returns:

  • Extracted value or empty string



84
85
86
87
88
89
# File 'lib/language_operator/cli/commands/agent/helpers/code_parser.rb', line 84

def extract_string_value(code, key)
  # Match both single and double quoted strings, including multi-line
  match = code.match(/#{key}:\s*(['"])(.*?)\1/m) ||
          code.match(/#{key}:\s*(['"])(.+?)\1/m)
  match ? match[2] : ''
end

#load_agent_definition(ctx, agent_name) ⇒ Object?

Load agent definition from ConfigMap

Parameters:

  • Cluster context

  • Name of the agent

Returns:

  • Agent definition or nil if not found



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/language_operator/cli/commands/agent/helpers/code_parser.rb', line 15

def load_agent_definition(ctx, agent_name)
  # Try to get the agent code ConfigMap
  configmap_name = "#{agent_name}-code"
  begin
    configmap = ctx.client.get_resource('ConfigMap', configmap_name, ctx.namespace)
    code_content = configmap.dig('data', 'agent.rb')

    return nil unless code_content

    # Parse the code to extract agent definition
    # For now, we'll create a mock definition with the task structure
    # In a full implementation, this would eval the code safely
    parse_agent_code(code_content)
  rescue K8s::Error::NotFound
    nil
  rescue StandardError => e
    @logger&.error("Failed to load agent definition: #{e.message}")
    nil
  end
end

#parse_agent_code(code) ⇒ Object

Parse agent code to extract definition

Parameters:

  • Ruby agent code

Returns:

  • Agent definition structure



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/language_operator/cli/commands/agent/helpers/code_parser.rb', line 40

def parse_agent_code(code)
  require_relative '../../../../dsl/agent_definition'

  # Create a minimal agent definition structure
  agent_def = Struct.new(:tasks, :name, :mcp_servers) do
    def initialize
      super({}, 'agent', {})
    end
  end

  agent = agent_def.new

  # Parse tasks from code - extract full task definitions
  code.scan(/task\s+:(\w+),?\s*(.*?)(?=\n\s*(?:task\s+:|main\s+do|end\s*$))/m) do |match|
    task_name = match[0].to_sym
    task_block = match[1]

    # Check if neural (has instructions but no do block) or symbolic
    is_neural = task_block.include?('instructions:') && !task_block.match?(/\bdo\s*\|/)

    # Extract instructions
    instructions = extract_string_value(task_block, 'instructions')

    # Extract inputs hash
    inputs = extract_hash_value(task_block, 'inputs')

    # Extract outputs hash
    outputs = extract_hash_value(task_block, 'outputs')

    task = Struct.new(:name, :neural?, :instructions, :inputs, :outputs).new(
      task_name, is_neural, instructions, inputs, outputs
    )

    agent.tasks[task_name] = task
  end

  agent
end