agent2agentSourceA2AProtoself

class << self

Definitions

def reset!

Reset cached state (for tests).

Implementation

def reset!
  @operations = nil
end

def extract_service_block(text)

Extract the body of service A2AService { ... } from the proto text.

Implementation

def extract_service_block(text)
  start = text.index(/^service\s+\w+\s*\{/)
  return "" unless start

  depth = 0
  pos = text.index("{", start)
  body_start = pos + 1

  (pos...text.length).each do |i|
    case text[i]
    when "{" then depth += 1
    when "}"
      depth -= 1
      return text[body_start...i] if depth == 0
    end
  end

  ""
end

def parse_rpcs(block)

Parse all rpc definitions from the service block text.

Implementation

def parse_rpcs(block)
  ops = []

  rpc_chunks = block.split(/(?=^\s*rpc\s)/m).select { |c| c.match?(/^\s*rpc\s/) }

  rpc_chunks.each do |chunk|
    sig = chunk.match(
      /rpc\s+(\w+)\s*\(\s*(\w[\w.]*)\s*\)\s*returns\s*\(\s*(stream\s+)?(\w[\w.]*)\s*\)/
    )
    next unless sig

    name           = sig[1]
    request_type   = sig[2]
    streaming      = !sig[3].nil?
    response_type  = sig[4]

    http_bindings = parse_http_bindings(chunk)

    ops << Operation.new(
      name:             name,
      request_type:     request_type,
      response_type:    response_type,
      server_streaming: streaming,
      http_bindings:    http_bindings,
    )
  end

  ops
end