Class: Legion::CLI::ObserveCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/observe_command.rb

Instance Method Summary collapse

Instance Method Details

#embeddingsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/cli/observe_command.rb', line 76

def embeddings
  require 'legion/mcp/embedding_index'
  data = {
    index_size: Legion::MCP::EmbeddingIndex.size,
    coverage:   Legion::MCP::EmbeddingIndex.coverage,
    populated:  Legion::MCP::EmbeddingIndex.populated?
  }

  if options['json']
    puts ::JSON.pretty_generate(data.transform_keys(&:to_s))
    return
  end

  puts 'MCP Embedding Index'
  puts '=' * 40
  puts "Index Size: #{data[:index_size]}"
  puts "Coverage:   #{(data[:coverage] * 100).round(1)}%"
  puts "Populated:  #{data[:populated]}"
end

#recentObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/cli/observe_command.rb', line 43

def recent
  calls = Legion::MCP::Observer.recent(options['limit'] || 20)

  if options['json']
    puts ::JSON.pretty_generate(calls.map { |c| serialize_call(c) })
    return
  end

  if calls.empty?
    puts 'No recent tool calls recorded.'
    return
  end

  puts 'Tool                           Duration  Status Time'
  puts '-' * 70
  calls.reverse_each do |call|
    status = call[:success] ? 'OK' : 'FAIL'
    time = call[:timestamp]&.strftime('%H:%M:%S')
    puts format('%-30<tool>s %6<dur>dms %7<st>s %<tm>s',
                tool: call[:tool_name], dur: call[:duration_ms], st: status, tm: time)
  end
end

#resetObject



67
68
69
70
71
72
73
# File 'lib/legion/cli/observe_command.rb', line 67

def reset
  print 'Clear all observation data? (yes/no): '
  return unless $stdin.gets&.strip&.downcase == 'yes'

  Legion::MCP::Observer.reset!
  puts 'Observation data cleared.'
end

#statsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/cli/observe_command.rb', line 12

def stats
  data = Legion::MCP::Observer.stats

  if options['json']
    puts ::JSON.pretty_generate(serialize_stats(data))
    return
  end

  puts 'MCP Tool Observation Stats'
  puts '=' * 40
  puts "Total Calls:  #{data[:total_calls]}"
  puts "Tools Used:   #{data[:tool_count]}"
  puts "Failure Rate: #{(data[:failure_rate] * 100).round(1)}%"
  puts "Since:        #{data[:since]&.strftime('%Y-%m-%d %H:%M:%S')}"
  puts

  return if data[:top_tools].empty?

  puts 'Top Tools:'
  puts '-' * 60
  puts 'Tool                            Calls  Avg(ms)  Fails'
  puts '-' * 60
  data[:top_tools].each do |tool|
    puts format('%-30<name>s %6<calls>d %8<avg>d %6<fails>d',
                name: tool[:name], calls: tool[:call_count],
                avg: tool[:avg_latency_ms], fails: tool[:failure_count])
  end
end